Last active
September 19, 2023 17:01
-
-
Save tkgregory/995dbedbc30b3132d0559cde63f273e6 to your computer and use it in GitHub Desktop.
An example CloudFormation template for deployment of a container to ECS, including integration with an Application Load Balancer.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AWSTemplateFormatVersion: "2010-09-09" | |
Parameters: | |
Subnet1ID: | |
Type: String | |
Subnet2ID: | |
Type: String | |
VPCID: | |
Type: String | |
Resources: | |
Cluster: | |
Type: AWS::ECS::Cluster | |
Properties: | |
ClusterName: deployment-example-cluster | |
LogGroup: | |
Type: AWS::Logs::LogGroup | |
Properties: | |
LogGroupName: deployment-example-log-group | |
ExecutionRole: | |
Type: AWS::IAM::Role | |
Properties: | |
RoleName: deployment-example-role | |
AssumeRolePolicyDocument: | |
Statement: | |
- Effect: Allow | |
Principal: | |
Service: ecs-tasks.amazonaws.com | |
Action: sts:AssumeRole | |
ManagedPolicyArns: | |
- arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy | |
ContainerSecurityGroup: | |
Type: AWS::EC2::SecurityGroup | |
Properties: | |
GroupName: ContainerSecurityGroup | |
GroupDescription: Security group for NGINX container | |
SecurityGroupIngress: | |
- IpProtocol: tcp | |
FromPort: 80 | |
ToPort: 80 | |
CidrIp: 0.0.0.0/0 | |
TaskDefinition: | |
Type: AWS::ECS::TaskDefinition | |
Properties: | |
Family: deployment-example-task | |
Cpu: 256 | |
Memory: 512 | |
NetworkMode: awsvpc | |
ExecutionRoleArn: !Ref ExecutionRole | |
ContainerDefinitions: | |
- Name: deployment-example-container | |
Image: nginx:1.17.7 | |
PortMappings: | |
- ContainerPort: 80 | |
LogConfiguration: | |
LogDriver: awslogs | |
Options: | |
awslogs-region: !Ref AWS::Region | |
awslogs-group: !Ref LogGroup | |
awslogs-stream-prefix: ecs | |
RequiresCompatibilities: | |
- EC2 | |
- FARGATE | |
Service: | |
Type: AWS::ECS::Service | |
Properties: | |
ServiceName: deployment-example-service | |
Cluster: !Ref Cluster | |
TaskDefinition: !Ref TaskDefinition | |
DesiredCount: 2 | |
LaunchType: FARGATE | |
NetworkConfiguration: | |
AwsvpcConfiguration: | |
AssignPublicIp: ENABLED | |
Subnets: | |
- !Ref Subnet1ID | |
- !Ref Subnet2ID | |
SecurityGroups: | |
- !GetAtt ContainerSecurityGroup.GroupId | |
LoadBalancers: | |
- TargetGroupArn: !Ref TargetGroup | |
ContainerPort: 80 | |
ContainerName: deployment-example-container | |
LoadBalancerSecurityGroup: | |
Type: AWS::EC2::SecurityGroup | |
Properties: | |
GroupName: LoadBalancerSecurityGroup | |
GroupDescription: Security group for load balancer | |
SecurityGroupIngress: | |
- IpProtocol: tcp | |
FromPort: 80 | |
ToPort: 80 | |
CidrIp: 0.0.0.0/0 | |
LoadBalancer: | |
Type: AWS::ElasticLoadBalancingV2::LoadBalancer | |
Properties: | |
Name: deployment-example-load-balancer | |
Subnets: | |
- !Ref Subnet1ID | |
- !Ref Subnet2ID | |
SecurityGroups: | |
- !GetAtt LoadBalancerSecurityGroup.GroupId | |
LoadBalancerListener: | |
Type: AWS::ElasticLoadBalancingV2::Listener | |
Properties: | |
LoadBalancerArn: !Ref LoadBalancer | |
Port: 80 | |
Protocol: HTTP | |
DefaultActions: | |
- Type: forward | |
TargetGroupArn: !Ref TargetGroup | |
TargetGroup: | |
Type: AWS::ElasticLoadBalancingV2::TargetGroup | |
Properties: | |
TargetType: ip | |
Name: deployment-example-target-group | |
Port: 80 | |
Protocol: HTTP | |
VpcId: !Ref VPCID |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment