Skip to content

Instantly share code, notes, and snippets.

@gucolin
Last active January 13, 2021 03:32
Show Gist options
  • Save gucolin/b9b108e5bcea513f9c704c576595f174 to your computer and use it in GitHub Desktop.
Save gucolin/b9b108e5bcea513f9c704c576595f174 to your computer and use it in GitHub Desktop.
Parameters:
Vpc:
Type: AWS::EC2::VPC::Id
Subnet1:
Description: Subnet ID of the public facing load balancer
Type: AWS::EC2::Subnet::Id
Subnet2:
Description: Subnet ID of the public facing load balancer
Type: AWS::EC2::Subnet::Id
ALBSecurityGroup:
Description: The Security Group Id for the Load Balancer (ex. sg-0b8908579dd834c73)
Type: AWS::EC2::SecurityGroup::Id
CertificateArn:
Description: The Arn of the SSL Certificate
Type: String
Transform:
- AWS::CodeDeployBlueGreen
Hooks:
CodeDeployBlueGreenHook:
Properties:
TrafficRoutingConfig:
Type: AllAtOnce
Applications:
- Target:
Type: 'AWS::ECS::Service'
LogicalID: ECSService
ECSAttributes:
TaskDefinitions:
- BlueTaskDefinition
- GreenTaskDefinition
TaskSets:
- BlueTaskSet
- GreenTaskSet
TrafficRouting:
ProdTrafficRoute:
Type: 'AWS::ElasticLoadBalancingV2::Listener'
LogicalID: ALBListenerProdTraffic
TargetGroups:
- ALBTargetGroupBlue
- ALBTargetGroupGreen
Type: 'AWS::CodeDeploy::BlueGreen'
Resources:
ALBTargetGroupBlue:
Type: 'AWS::ElasticLoadBalancingV2::TargetGroup'
Properties:
HealthCheckIntervalSeconds: 5
HealthCheckPath: /
HealthCheckProtocol: HTTP
HealthCheckTimeoutSeconds: 2
HealthyThresholdCount: 2
Matcher:
HttpCode: '200'
Port: 8000
Protocol: HTTP
TargetType: instance
UnhealthyThresholdCount: 4
VpcId: !Ref Vpc
ALBTargetGroupGreen:
Type: 'AWS::ElasticLoadBalancingV2::TargetGroup'
Properties:
HealthCheckIntervalSeconds: 5
HealthCheckPath: /
HealthCheckProtocol: HTTP
HealthCheckTimeoutSeconds: 2
HealthyThresholdCount: 2
Matcher:
HttpCode: '200'
Port: 8000
Protocol: HTTP
TargetType: instance
UnhealthyThresholdCount: 4
VpcId: !Ref Vpc
LoadBalancer:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
Scheme: internet-facing
SecurityGroups:
- !Ref ALBSecurityGroup
Subnets:
- !Ref Subnet1
- !Ref Subnet2
Type: application
IpAddressType: ipv4
ALBListenerProdTraffic:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
DefaultActions:
- Type: forward
ForwardConfig:
TargetGroups:
- TargetGroupArn: !Ref ALBTargetGroupBlue
Weight: 1
LoadBalancerArn: !Ref LoadBalancer
Certificates:
- CertificateArn: !Ref CertificateArn
Port: 443
Protocol: HTTPS
ALBListener80Forward:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
DefaultActions:
- Type: redirect
RedirectConfig:
Protocol: HTTPS
Port: '443'
Host: '#{host}'
Path: /#{path}
Query: '#{query}'
StatusCode: HTTP_301
LoadBalancerArn: !Ref LoadBalancer
Port: 80
Protocol: HTTP
ECSTaskExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Sid: ''
Effect: Allow
Principal:
Service: ecs-tasks.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy
BlueTaskDefinition:
Type: 'AWS::ECS::TaskDefinition'
Properties:
ExecutionRoleArn: !GetAtt
- ECSTaskExecutionRole
- Arn
ContainerDefinitions:
- Name: public-container
Image: 'image-url'
Essential: true
PortMappings:
- Protocol: tcp
ContainerPort: 8000
- Name: private-container
Image: 'image-url'
Essential: true
NetworkMode: bridge
Cpu: '256'
Memory: '512'
Family: 'family'
ECSService:
Type: 'AWS::ECS::Service'
Properties:
Cluster: 'service-cluster'
DesiredCount: 1
DeploymentController:
Type: EXTERNAL
BlueTaskSet:
Type: 'AWS::ECS::TaskSet'
Properties:
Cluster: 'service-cluster'
LaunchType: EC2
# PlatformVersion: 1.3.0 platform version must be null when launch type is ec2
Scale:
Unit: PERCENT
Value: 1
Service: !Ref ECSService
TaskDefinition: !Ref BlueTaskDefinition
LoadBalancers:
- ContainerName: public-container
ContainerPort: 8000
TargetGroupArn: !Ref ALBTargetGroupBlue
PrimaryTaskSet:
Type: AWS::ECS::PrimaryTaskSet
Properties:
Cluster: service-cluster
Service: !Ref ECSService
TaskSetId: !GetAtt
- BlueTaskSet
- Id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment