Got the codepipeline nearly done, need to get the updated docker file in main.

This commit is contained in:
Joe Bailey 2023-02-12 17:42:59 -08:00
parent 008f84a252
commit 426463660a
4 changed files with 80 additions and 199 deletions

View file

@ -1,3 +1,3 @@
FROM openjdk:8-jdk-alpine FROM openjdk:20-ea-17-jdk
COPY target/spring-petclinic-3.0.0-SNAPSHOT.jar spring-petclinic-3.0.0-SNAPSHOT.jar COPY target/spring-petclinic-3.0.0-SNAPSHOT.jar spring-petclinic-3.0.0-SNAPSHOT.jar
ENTRYPOINT ["java","-jar","/spring-petclinic-3.0.0-SNAPSHOT.jar"] ENTRYPOINT ["java","-jar","/spring-petclinic-3.0.0-SNAPSHOT.jar"]

View file

@ -29,6 +29,7 @@ Parameters:
Type: String Type: String
Resources: Resources:
BlueGreenFlipLambda: BlueGreenFlipLambda:
Type: AWS::Lambda::Function Type: AWS::Lambda::Function
Properties: Properties:
@ -44,19 +45,20 @@ Resources:
Role: !GetAtt BlueGreenFlipLambdaRole.Arn Role: !GetAtt BlueGreenFlipLambdaRole.Arn
BlueGreenFlipLambdaRole: BlueGreenFlipLambdaRole:
Type: AWS::IAM::Role Type: AWS::IAM::Role
Properties: Properties:
#RoleName: !Sub lambda-${GitHubRepo}-blue-green-swap-role #RoleName: !Sub lambda-${GitHubRepo}-blue-green-swap-role
AssumeRolePolicyDocument: AssumeRolePolicyDocument:
Version: "2012-10-17" Version: "2012-10-17"
Statement: Statement:
- Effect: Allow -
Principal: Effect: Allow
Service: Principal:
- lambda.amazonaws.com Service:
Action: - lambda.amazonaws.com
- sts:AssumeRole Action:
Path: / - sts:AssumeRole
Path: /
BlueGreenFlipLambdaPolicy: BlueGreenFlipLambdaPolicy:
Type: AWS::IAM::Policy Type: AWS::IAM::Policy
@ -65,20 +67,23 @@ Resources:
PolicyDocument: PolicyDocument:
Version: "2012-10-17" Version: "2012-10-17"
Statement: Statement:
- Effect: Allow -
Effect: Allow
Action: Action:
- elasticloadbalancing:* - elasticloadbalancing:*
- codepipeline:PutJobFailureResult - codepipeline:PutJobFailureResult
- codepipeline:PutJobSuccessResult - codepipeline:PutJobSuccessResult
Resource: "*" Resource: "*"
- Effect: Allow -
Effect: Allow
Action: Action:
- logs:CreateLogGroup - logs:CreateLogGroup
- logs:CreateLogStream - logs:CreateLogStream
- logs:PutLogEvents - logs:PutLogEvents
Resource: arn:aws:logs:*:*:* Resource: arn:aws:logs:*:*:*
Roles: Roles:
- !Ref BlueGreenFlipLambdaRole -
!Ref BlueGreenFlipLambdaRole
Repository: Repository:
Type: AWS::ECR::Repository Type: AWS::ECR::Repository
@ -212,10 +217,9 @@ Resources:
phases: phases:
# install: # install:
# commands: # commands:
# - apt-get update -y # - apt-get update && apt-get -y install python-pip
# - apt-get -y install python-pip # - pip install --upgrade python
# - pip install --upgrade python # - pip install --upgrade awscli
# - pip install --upgrade awscli
pre_build: pre_build:
commands: commands:
- printenv - printenv
@ -259,9 +263,9 @@ Resources:
phases: phases:
# install: # install:
# commands: # commands:
# - pip install --upgrade python # - pip install --upgrade python
# - pip install --upgrade awscli # - pip install --upgrade awscli
# - pip install --upgrade boto3 # - pip install --upgrade boto3
pre_build: pre_build:
commands: commands:
- printenv - printenv
@ -413,6 +417,8 @@ Resources:
} }
RunOrder: 3 RunOrder: 3
Outputs: Outputs:
PipelineUrl: PipelineUrl:
Value: !Sub https://console.aws.amazon.com/codepipeline/home?region=${AWS::Region}#/view/${Pipeline} Value: !Sub https://console.aws.amazon.com/codepipeline/home?region=${AWS::Region}#/view/${Pipeline}

View file

@ -41,137 +41,13 @@ Parameters:
S3 Bucket used for nested templates S3 Bucket used for nested templates
Mappings:
AWSRegionToAMI:
us-east-1:
AMI: ami-71ef560b
us-east-2:
AMI: ami-1b8ca37e
us-west-1:
AMI: ami-e5cdf385
us-west-2:
AMI: ami-a64d9ade
eu-west-1:
AMI: ami-014ae578
eu-west-2:
AMI: ami-4f8d912b
eu-central-1:
AMI: ami-4255d32d
ap-northeast-1:
AMI: ami-3405af52
ap-southeast-1:
AMI: ami-134e0670
ap-southeast-2:
AMI: ami-2ab95148
ca-central-1:
AMI: ami-c802baac
Resources: Resources:
ECSRole:
Type: AWS::IAM::Role
Properties:
Path: /
#RoleName: !Sub ecs-${AWS::StackName}
AssumeRolePolicyDocument: |
{
"Statement": [{
"Effect": "Allow",
"Principal": { "Service": [ "ec2.amazonaws.com" ]},
"Action": [ "sts:AssumeRole" ]
}]
}
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role
InstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Path: /
Roles:
- !Ref ECSRole
SecurityGroup:
Type: "AWS::EC2::SecurityGroup"
Properties:
GroupDescription: !Sub ${AWS::StackName}-hosts
SecurityGroupIngress:
- SourceSecurityGroupId: !GetAtt LoadBalancer.Outputs.SecurityGroup
IpProtocol: -1
VpcId: !Ref VpcId
Cluster: Cluster:
Type: AWS::ECS::Cluster Type: AWS::ECS::Cluster
Properties: Properties:
ClusterName: !Ref AWS::StackName ClusterName: !Ref AWS::StackName
AutoScalingGroup:
Type: AWS::AutoScaling::AutoScalingGroup
Properties:
VPCZoneIdentifier:
- !Ref Subnet1
- !Ref Subnet2
LaunchConfigurationName: !Ref LaunchConfiguration
MinSize: !Ref ClusterSize
MaxSize: !Ref ClusterSize
DesiredCapacity: !Ref ClusterSize
Tags:
- Key: Name
Value: !Sub ${AWS::StackName} - ECS Host
PropagateAtLaunch: true
CreationPolicy:
ResourceSignal:
Timeout: PT15M
UpdatePolicy:
AutoScalingRollingUpdate:
MinInstancesInService: 1
MaxBatchSize: 1
PauseTime: PT15M
WaitOnResourceSignals: true
LaunchConfiguration:
Type: AWS::AutoScaling::LaunchConfiguration
Metadata:
AWS::CloudFormation::Init:
config:
commands:
01_add_instance_to_cluster:
command: !Sub echo ECS_CLUSTER=${Cluster} > /etc/ecs/ecs.config
files:
"/etc/cfn/cfn-hup.conf":
mode: 000400
owner: root
group: root
content: !Sub |
[main]
stack=${AWS::StackId}
region=${AWS::Region}
"/etc/cfn/hooks.d/cfn-auto-reloader.conf":
content: !Sub |
[cfn-auto-reloader-hook]
triggers=post.update
path=Resources.ContainerInstances.Metadata.AWS::CloudFormation::Init
action=/opt/aws/bin/cfn-init -v --region ${AWS::Region} --stack ${AWS::StackName} --resource LaunchConfiguration
services:
sysvinit:
cfn-hup:
enabled: true
ensureRunning: true
files:
- /etc/cfn/cfn-hup.conf
- /etc/cfn/hooks.d/cfn-auto-reloader.conf
Properties:
ImageId: !FindInMap [ AWSRegionToAMI, !Ref "AWS::Region", AMI ]
InstanceType: !Ref InstanceType
IamInstanceProfile: !Ref InstanceProfile
SecurityGroups:
- !Ref SecurityGroup
UserData:
"Fn::Base64": !Sub |
#!/bin/bash
yum install -y aws-cfn-bootstrap
/opt/aws/bin/cfn-init -v --region ${AWS::Region} --stack ${AWS::StackName} --resource LaunchConfiguration
/opt/aws/bin/cfn-signal -e $? --region ${AWS::Region} --stack ${AWS::StackName} --resource AutoScalingGroup
LoadBalancer: LoadBalancer:
Type: AWS::CloudFormation::Stack Type: AWS::CloudFormation::Stack
@ -198,6 +74,9 @@ Resources:
Repository: !Ref Repository Repository: !Ref Repository
Identifier: "Code2" Identifier: "Code2"
IsProduction: False IsProduction: False
Subnet1 : !Ref Subnet1
Subnet2 : !Ref Subnet2
ALBSg : !GetAtt LoadBalancer.Outputs.SecurityGroup
BlueService: BlueService:
Type: AWS::CloudFormation::Stack Type: AWS::CloudFormation::Stack
@ -213,6 +92,9 @@ Resources:
Repository: !Ref Repository Repository: !Ref Repository
Identifier: "Code1" Identifier: "Code1"
IsProduction: True IsProduction: True
Subnet1 : !Ref Subnet1
Subnet2 : !Ref Subnet2
ALBSg : !GetAtt LoadBalancer.Outputs.SecurityGroup
Outputs: Outputs:

View file

@ -31,6 +31,15 @@ Parameters:
IsProduction: IsProduction:
Type: String Type: String
Subnet1:
Type: String
Subnet2:
Type: String
ALBSg:
Type: String
Resources: Resources:
LoadBalancerListener: LoadBalancerListener:
Type: AWS::ElasticLoadBalancingV2::Listener Type: AWS::ElasticLoadBalancingV2::Listener
@ -46,6 +55,7 @@ Resources:
Type: AWS::ElasticLoadBalancingV2::TargetGroup Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties: Properties:
VpcId: !Ref VpcId VpcId: !Ref VpcId
TargetType: ip
Port: !Ref Port Port: !Ref Port
Protocol: HTTP Protocol: HTTP
Matcher: Matcher:
@ -79,87 +89,70 @@ Resources:
ListenerArn: !Ref LoadBalancerListener ListenerArn: !Ref LoadBalancerListener
Priority: 1 Priority: 1
ECSServiceRole: TaskIamRole:
Type: AWS::IAM::Role Type: AWS::IAM::Role
Properties: Properties:
#RoleName: !Sub ecs-service-${AWS::StackName}
Path: / Path: /
AssumeRolePolicyDocument: | AssumeRolePolicyDocument: |
{ {
"Statement": [{ "Statement": [{
"Effect": "Allow", "Effect": "Allow",
"Principal": { "Service": [ "ecs.amazonaws.com" ]}, "Principal": { "Service": [ "ecs-tasks.amazonaws.com" ]},
"Action": [ "sts:AssumeRole" ] "Action": [ "sts:AssumeRole" ]
}] }]
} }
ManagedPolicyArns: ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceRole - arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly
- arn:aws:iam::aws:policy/CloudWatchLogsFullAccess
SecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: !Sub ${AWS::StackName}
SecurityGroupIngress:
- SourceSecurityGroupId: !Ref ALBSg
IpProtocol: -1
VpcId: !Ref VpcId
Service: Service:
Type: AWS::ECS::Service Type: AWS::ECS::Service
Properties: Properties:
Cluster: !Ref Cluster Cluster: !Ref Cluster
Role: !Ref ECSServiceRole
DesiredCount: !Ref DesiredCount DesiredCount: !Ref DesiredCount
LaunchType: FARGATE
TaskDefinition: !Ref TaskDefinition TaskDefinition: !Ref TaskDefinition
# CapacityProviderStrategy:
# - CapacityProvider: FARGATE
# - Base: 0
# - Weight: 1
LoadBalancers: LoadBalancers:
- ContainerName: simple-app - ContainerName: simple-app
ContainerPort: 80 ContainerPort: 8080
TargetGroupArn: !Ref TargetGroup TargetGroupArn: !Ref TargetGroup
# NetworkConfiguration: NetworkConfiguration:
# - AwsvpcConfiguration: AwsvpcConfiguration:
# - AssignPublicIp: ENABLED AssignPublicIp: ENABLED #MENTION DISABLED if in private subnet with NAT gateway
# - SecurityGroups: SecurityGroups:
# - Ref: SecurityGroupIDs - !GetAtt SecurityGroup.GroupId
# - Subnets: !!omap Subnets:
# - Ref: SubnetIDs - !Ref Subnet1
PlatformVersion: LATEST - !Ref Subnet2
# DeploymentConfiguration:
# - MaximumPercent: 200
# - MinimumHealthyPercent: 100
# - DeploymentCircuitBreaker:
# - Enable: true
# - Rollback: true
# DeploymentController:
# - Type: ECS
# ServiceConnectConfiguration:
# - Enabled: false
TaskDefinition: TaskDefinition:
Type: AWS::ECS::TaskDefinition Type: AWS::ECS::TaskDefinition
Properties: Properties:
Family: !Sub ${AWS::StackName}-simple-app Cpu: 512
Memory: 512
NetworkMode: awsvpc
RequiresCompatibilities:
- FARGATE
ExecutionRoleArn: !GetAtt TaskIamRole.Arn
ContainerDefinitions: ContainerDefinitions:
- Name: simple-app - Name: simple-app
Image: !Sub ${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com/${Repository}:${Tag} Image: !Sub ${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com/${Repository}:${Tag}
EntryPoint: # EntryPoint:
- /usr/sbin/apache2 # - /usr/sbin/apache2
- -D # - -D
- FOREGROUND # - FOREGROUND
Essential: true Essential: true
Memory: 128 Memory: 512
MountPoints:
- SourceVolume: my-vol
ContainerPath: /var/www/my-vol
PortMappings: PortMappings:
- ContainerPort: 80 - ContainerPort: 8080
Environment: Environment:
- Name: Tag - Name: Tag
Value: !Ref Tag Value: !Ref Tag
- Name: busybox
Image: busybox
EntryPoint:
- sh
- -c
Essential: false
Memory: 128
VolumesFrom:
- SourceContainer: simple-app
Command:
- /bin/sh -c "while true; do /bin/date > /var/www/my-vol/date; sleep 1; done"
Volumes:
- Name: my-vol