$npx -y skills add giuseppe-trisciuoglio/developer-kit --skill aws-cloudformation-ecsProvides AWS CloudFormation patterns for ECS clusters, task definitions, services, container definitions, auto scaling, blue/green deployments, CodeDeploy integration, ALB integration, service discovery, monitoring, logging, template structure, parameters, outputs, and cross-stac
| 1 | # AWS CloudFormation ECS |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | Provides CloudFormation patterns for ECS clusters, task definitions, services, container definitions, auto scaling, blue/green deployments, ALB integration, monitoring, and cross-stack references. |
| 6 | |
| 7 | ## When to Use |
| 8 | |
| 9 | - Creating or updating ECS clusters with CloudFormation |
| 10 | - Configuring Fargate/EC2 launch types and capacity providers |
| 11 | - Deploying services with ALB/NLB integration or blue/green deployments |
| 12 | - Implementing auto scaling for ECS services |
| 13 | - Setting up monitoring with Container Insights |
| 14 | |
| 15 | ## Instructions |
| 16 | |
| 17 | Follow these steps to create ECS infrastructure with CloudFormation: |
| 18 | |
| 19 | ### 1. Define ECS Cluster Parameters |
| 20 | |
| 21 | Specify launch type, networking, and capacity settings: |
| 22 | |
| 23 | ```yaml |
| 24 | Parameters: |
| 25 | LaunchType: |
| 26 | Type: String |
| 27 | Default: FARGATE |
| 28 | AllowedValues: |
| 29 | - EC2 |
| 30 | - FARGATE |
| 31 | Description: ECS launch type |
| 32 | |
| 33 | ContainerPort: |
| 34 | Type: Number |
| 35 | Default: 80 |
| 36 | Description: Container port |
| 37 | |
| 38 | TaskCPU: |
| 39 | Type: String |
| 40 | Default: 256 |
| 41 | AllowedValues: |
| 42 | - 256 |
| 43 | - 512 |
| 44 | - 1024 |
| 45 | - 2048 |
| 46 | - 4096 |
| 47 | Description: Task CPU units |
| 48 | |
| 49 | TaskMemory: |
| 50 | Type: String |
| 51 | Default: 512 |
| 52 | AllowedValues: |
| 53 | - 512 |
| 54 | - 1024 |
| 55 | - 2048 |
| 56 | - 3072 |
| 57 | - 4096 |
| 58 | - 5120 |
| 59 | - 6144 |
| 60 | - 7168 |
| 61 | - 8192 |
| 62 | - 9216 |
| 63 | - 10240 |
| 64 | Description: Task memory in MB |
| 65 | ``` |
| 66 | |
| 67 | ### 2. Create ECS Cluster |
| 68 | |
| 69 | Define the cluster infrastructure: |
| 70 | |
| 71 | ```yaml |
| 72 | Resources: |
| 73 | ECSCluster: |
| 74 | Type: AWS::ECS::Cluster |
| 75 | Properties: |
| 76 | ClusterName: !Sub "${AWS::StackName}-cluster" |
| 77 | ClusterSettings: |
| 78 | - Name: containerInsights |
| 79 | Value: enabled |
| 80 | CapacityProviders: |
| 81 | - FARGATE |
| 82 | - FARGATE_SPOT |
| 83 | DefaultCapacityProviderStrategy: |
| 84 | - CapacityProvider: FARGATE |
| 85 | Weight: 1 |
| 86 | - CapacityProvider: FARGATE_SPOT |
| 87 | Weight: 0 |
| 88 | ``` |
| 89 | |
| 90 | ### 3. Create Task Definition |
| 91 | |
| 92 | Define container configurations: |
| 93 | |
| 94 | ```yaml |
| 95 | Resources: |
| 96 | TaskDefinition: |
| 97 | Type: AWS::ECS::TaskDefinition |
| 98 | Properties: |
| 99 | Family: !Sub "${AWS::StackName}-task" |
| 100 | NetworkMode: awsvpc |
| 101 | RequiresCompatibilities: |
| 102 | - FARGATE |
| 103 | Cpu: !Ref TaskCPU |
| 104 | Memory: !Ref TaskMemory |
| 105 | ExecutionRoleArn: !Ref ExecutionRole |
| 106 | TaskRoleArn: !Ref TaskRole |
| 107 | ContainerDefinitions: |
| 108 | - Name: application |
| 109 | Image: !Ref ImageUrl |
| 110 | PortMappings: |
| 111 | - ContainerPort: !Ref ContainerPort |
| 112 | Protocol: tcp |
| 113 | Environment: |
| 114 | - Name: LOG_LEVEL |
| 115 | Value: INFO |
| 116 | LogConfiguration: |
| 117 | LogDriver: awslogs |
| 118 | Options: |
| 119 | awslogs-group: !Ref LogGroup |
| 120 | awslogs-region: !Ref AWS::Region |
| 121 | awslogs-stream-prefix: ecs |
| 122 | Memory: !Ref TaskMemory |
| 123 | ``` |
| 124 | |
| 125 | **Validate task definition syntax before proceeding:** |
| 126 | ```bash |
| 127 | aws cloudformation validate-template --template-body file://template.yaml |
| 128 | ``` |
| 129 | |
| 130 | ### 4. Configure Execution Roles |
| 131 | |
| 132 | Set up IAM roles for task execution: |
| 133 | |
| 134 | ```yaml |
| 135 | Resources: |
| 136 | ExecutionRole: |
| 137 | Type: AWS::IAM::Role |
| 138 | Properties: |
| 139 | AssumeRolePolicyDocument: |
| 140 | Version: "2012-10-17" |
| 141 | Statement: |
| 142 | - Effect: Allow |
| 143 | Principal: |
| 144 | Service: ecs-tasks.amazonaws.com |
| 145 | Action: sts:AssumeRole |
| 146 | ManagedPolicyArns: |
| 147 | - arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy |
| 148 | |
| 149 | TaskRole: |
| 150 | Type: AWS::IAM::Role |
| 151 | Properties: |
| 152 | AssumeRolePolicyDocument: |
| 153 | Version: "2012-10-17" |
| 154 | Statement: |
| 155 | - Effect: Allow |
| 156 | Principal: |
| 157 | Service: ecs-tasks.amazonaws.com |
| 158 | Action: sts:AssumeRole |
| 159 | Policies: |
| 160 | - PolicyName: S3Access |
| 161 | PolicyDocument: |
| 162 | Version: "2012-10-17" |
| 163 | Statement: |
| 164 | - Effect: Allow |
| 165 | Action: |
| 166 | - s3:GetObject |
| 167 | Resource: !Sub "${DataBucket.Arn}/*" |
| 168 | ``` |
| 169 | |
| 170 | ### 5. Create ECS Service |
| 171 | |
| 172 | Define the service configuration: |
| 173 | |
| 174 | ```yaml |
| 175 | Resources: |
| 176 | ECSService: |
| 177 | Type: AWS::ECS::Service |
| 178 | Properties: |
| 179 | ServiceName: !Sub "${AWS::StackName}-service" |
| 180 | Cluster: !Ref ECSCluster |
| 181 | TaskDefinition: !Ref TaskDefinition |
| 182 | DesiredCount: 2 |
| 183 | LaunchType: FARGATE |
| 184 | NetworkConfiguration: |
| 185 | AwsvpcConfiguration: |
| 186 | Subnets: |
| 187 | - !Ref PrivateSubnet1 |
| 188 | - !Ref PrivateSubnet2 |
| 189 | Se |