$npx -y skills add giuseppe-trisciuoglio/developer-kit --skill aws-cloudformation-ec2Provides AWS CloudFormation patterns for EC2 instances, Security Groups, IAM roles, and load balancers. Use when creating EC2 instances, SPOT instances, Security Groups, IAM roles for EC2, Application Load Balancers (ALB), Target Groups, and implementing template structure with P
| 1 | # AWS CloudFormation EC2 Infrastructure |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | Create production-ready EC2 infrastructure using AWS CloudFormation templates. Covers EC2 instances (On-Demand and SPOT), Security Groups, IAM roles, Application Load Balancers (ALB), template structure, parameters, outputs, and cross-stack references. |
| 6 | |
| 7 | ## When to Use |
| 8 | |
| 9 | - Creating EC2 instances (On-Demand or SPOT) with Security Groups and IAM roles |
| 10 | - Setting up Application Load Balancers with target groups |
| 11 | - Implementing template Parameters, Mappings, Conditions, and cross-stack references |
| 12 | |
| 13 | ## Instructions |
| 14 | |
| 15 | ### Step 1 — Define Template Parameters |
| 16 | |
| 17 | Use AWS-specific parameter types for validation and console dropdowns. |
| 18 | |
| 19 | ```yaml |
| 20 | Parameters: |
| 21 | LatestAmiId: |
| 22 | Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id> |
| 23 | Default: /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2 |
| 24 | |
| 25 | InstanceType: |
| 26 | Type: AWS::EC2::InstanceType |
| 27 | Default: t3.micro |
| 28 | AllowedValues: [t3.micro, t3.small, t3.medium] |
| 29 | |
| 30 | KeyName: |
| 31 | Type: AWS::EC2::KeyPair::KeyName |
| 32 | ``` |
| 33 | |
| 34 | See [template-structure.md](references/template-structure.md) for advanced parameter patterns, mappings, conditions, and cross-stack references. |
| 35 | |
| 36 | ### Step 2 — Create Security Group |
| 37 | |
| 38 | Define ingress/egress rules for network access. |
| 39 | |
| 40 | ```yaml |
| 41 | InstanceSecurityGroup: |
| 42 | Type: AWS::EC2::SecurityGroup |
| 43 | Properties: |
| 44 | GroupDescription: Security group for EC2 instance |
| 45 | VpcId: !Ref VpcId |
| 46 | SecurityGroupIngress: |
| 47 | - IpProtocol: tcp |
| 48 | FromPort: 80 |
| 49 | ToPort: 80 |
| 50 | CidrIp: 0.0.0.0/0 |
| 51 | - IpProtocol: tcp |
| 52 | FromPort: 22 |
| 53 | ToPort: 22 |
| 54 | CidrIp: 10.0.0.0/16 |
| 55 | ``` |
| 56 | |
| 57 | See [security-iam.md](references/security-iam.md) for advanced security group patterns, self-references, and IAM roles. |
| 58 | |
| 59 | ### Step 3 — Configure IAM Role |
| 60 | |
| 61 | Define instance profile with least privilege permissions. |
| 62 | |
| 63 | ```yaml |
| 64 | Ec2Role: |
| 65 | Type: AWS::IAM::Role |
| 66 | Properties: |
| 67 | AssumeRolePolicyDocument: |
| 68 | Version: "2012-10-17" |
| 69 | Statement: |
| 70 | - Effect: Allow |
| 71 | Principal: |
| 72 | Service: ec2.amazonaws.com |
| 73 | Action: sts:AssumeRole |
| 74 | ManagedPolicyArns: |
| 75 | - arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore |
| 76 | |
| 77 | Ec2InstanceProfile: |
| 78 | Type: AWS::IAM::InstanceProfile |
| 79 | Properties: |
| 80 | Roles: [!Ref Ec2Role] |
| 81 | ``` |
| 82 | |
| 83 | See [security-iam.md](references/security-iam.md) for least privilege policies, SSM roles, and trust policies. |
| 84 | |
| 85 | ### Step 4 — Launch EC2 Instance |
| 86 | |
| 87 | Configure instance with security group, IAM role, and user data. |
| 88 | |
| 89 | ```yaml |
| 90 | Ec2Instance: |
| 91 | Type: AWS::EC2::Instance |
| 92 | Properties: |
| 93 | ImageId: !Ref LatestAmiId |
| 94 | InstanceType: !Ref InstanceType |
| 95 | KeyName: !Ref KeyName |
| 96 | SecurityGroupIds: [!Ref InstanceSecurityGroup] |
| 97 | IamInstanceProfile: !Ref Ec2InstanceProfile |
| 98 | SubnetId: !Ref SubnetId |
| 99 | UserData: |
| 100 | Fn::Base64: | |
| 101 | #!/bin/bash |
| 102 | yum update -y |
| 103 | yum install -y httpd |
| 104 | systemctl start httpd |
| 105 | Tags: |
| 106 | - Key: Name |
| 107 | Value: !Sub ${AWS::StackName}-instance |
| 108 | ``` |
| 109 | |
| 110 | See [ec2-instances.md](references/ec2-instances.md) for multi-volume configurations, detailed monitoring, SPOT instances, and complete stack examples. |
| 111 | |
| 112 | **Validate template:** `aws cloudformation validate-template --template-body file://template.yaml` |
| 113 | |
| 114 | ### Step 5 — Add Application Load Balancer |
| 115 | |
| 116 | Create ALB with target group and listener for traffic distribution. |
| 117 | |
| 118 | ```yaml |
| 119 | ApplicationLoadBalancer: |
| 120 | Type: AWS::ElasticLoadBalancingV2::LoadBalancer |
| 121 | Properties: |
| 122 | Name: !Sub ${AWS::StackName}-alb |
| 123 | Scheme: internet-facing |
| 124 | SecurityGroups: [!Ref AlbSecurityGroup] |
| 125 | Subnets: [!Ref PublicSubnet1, !Ref PublicSubnet2] |
| 126 | |
| 127 | ApplicationTargetGroup: |
| 128 | Type: AWS::ElasticLoadBalancingV2::TargetGroup |
| 129 | Properties: |
| 130 | Port: 80 |
| 131 | Protocol: HTTP |
| 132 | VpcId: !Ref VpcId |
| 133 | HealthCheckPath: /health |
| 134 | |
| 135 | ApplicationListener: |
| 136 | Type: AWS::ElasticLoadBalancingV2::Listener |
| 137 | Properties: |
| 138 | DefaultActions: |
| 139 | - Type: forward |
| 140 | TargetGroupArn: !Ref ApplicationTargetGroup |
| 141 | LoadBalancerArn: !Ref ApplicationLoadBalancer |
| 142 | Port: 80 |
| 143 | Protocol: HTTP |
| 144 | ``` |
| 145 | |
| 146 | See [load-balancers.md](references/load-balancers.md) for HTTPS configuration, path-based routing, host-based routing, listener rules, and ALB attributes. |
| 147 | |
| 148 | ### Step 6 — Define Outputs |
| 149 | |
| 150 | Export values for cross-stack references. |
| 151 | |
| 152 | ```yaml |
| 153 | Outputs: |
| 154 | InstanceId: |
| 155 | Description: EC2 Instance ID |
| 156 | Value: !Ref Ec2Instance |
| 157 | Export: |
| 158 | Name: !Sub ${AWS::StackName}-InstanceId |
| 159 | |
| 160 | SecurityGroupId: |
| 161 | Description: Security Group ID |
| 162 | Value: !Ref InstanceSecurityGroup |
| 163 | Export: |
| 164 | Name: !Sub ${AWS::StackName}-SecurityGroupId |