|
| 1 | +--- |
| 2 | +AWSTemplateFormatVersion: "2010-09-09" |
| 3 | +Description: EC2 bastion for latest AWS Linux 2 EC2 deployment |
| 4 | +Metadata: |
| 5 | + AWS::CloudFormation::Interface: |
| 6 | + ParameterGroups: |
| 7 | + - Label: |
| 8 | + default: "EC2 Configuration" |
| 9 | + Parameters: |
| 10 | + - pTagNameValue |
| 11 | + - pOperatingSystem |
| 12 | + - pInstanceType |
| 13 | + - pVolumeSize |
| 14 | + - pEbsDeleteOnTermination |
| 15 | + - Label: |
| 16 | + default: "Network Configuration" |
| 17 | + Parameters: |
| 18 | + - pVpc |
| 19 | + - pSubnet |
| 20 | + ParameterLabels: |
| 21 | + pOperatingSystem: |
| 22 | + default: "Operating System" |
| 23 | + pInstanceType: |
| 24 | + default: "Instance Type" |
| 25 | + pTagNameValue: |
| 26 | + default: "EC2 Name" |
| 27 | + pVolumeSize: |
| 28 | + default: "Volume Size" |
| 29 | + pEbsDeleteOnTermination: |
| 30 | + default: "Delete EBS Volume on Termination" |
| 31 | + pSubnet: |
| 32 | + default: "Subnet" |
| 33 | + pVpc: |
| 34 | + default: "VPC" |
| 35 | +Parameters: |
| 36 | + pSubnet: |
| 37 | + Description: The subnet to launch the instance in to. It must be part of the VPC chosen above. |
| 38 | + Type: AWS::EC2::Subnet::Id |
| 39 | + pVpc: |
| 40 | + Description: The VPC to launch the EC2 instance in to. |
| 41 | + Type: AWS::EC2::VPC::Id |
| 42 | + pOperatingSystem: |
| 43 | + Type: "AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>" |
| 44 | + Default: "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-ebs" |
| 45 | + pInstanceType: |
| 46 | + Description: Desired Instance Size |
| 47 | + Type: String |
| 48 | + Default: t3.small |
| 49 | + AllowedValues: |
| 50 | + - t3.small |
| 51 | + - t3.medium |
| 52 | + - t3.nano |
| 53 | + pTagNameValue: |
| 54 | + Description: "Required: Enter the tag name you'd like applied to the instance. Tag Name gives the name to the EC2 instance." |
| 55 | + Type: String |
| 56 | + MinLength: 1 |
| 57 | + Default: "myBastion" |
| 58 | + pVolumeSize: |
| 59 | + Description: |
| 60 | + Enter the number of GBs you want your volume to be. The minimum value |
| 61 | + is 8 GBs |
| 62 | + Type: Number |
| 63 | + Default: 50 |
| 64 | + MinValue: 8 |
| 65 | + pEbsDeleteOnTermination: |
| 66 | + Description: "Specify if the EBS volume should be deleted if EC2 is deleted." |
| 67 | + Type: String |
| 68 | + Default: true |
| 69 | + AllowedValues: |
| 70 | + - true |
| 71 | + - false |
| 72 | +Rules: |
| 73 | + SubnetInVPC: |
| 74 | + Assertions: |
| 75 | + - Assert: !EachMemberIn |
| 76 | + - !ValueOfAll |
| 77 | + - AWS::EC2::Subnet::Id |
| 78 | + - VpcId |
| 79 | + - !RefAll "AWS::EC2::VPC::Id" |
| 80 | + AssertDescription: All subnets must in the VPC |
| 81 | +Resources: |
| 82 | + rSecurityGroupDefault: |
| 83 | + Type: AWS::EC2::SecurityGroup |
| 84 | + Properties: |
| 85 | + GroupDescription: !Sub "Default SG for SC Product ${pTagNameValue} " |
| 86 | + VpcId: !Ref pVpc |
| 87 | + SecurityGroupEgress: |
| 88 | + - Description: Outbound unrestricted traffic |
| 89 | + IpProtocol: "-1" |
| 90 | + CidrIp: 0.0.0.0/0 |
| 91 | + Tags: |
| 92 | + - Key: Name |
| 93 | + Value: !Ref pTagNameValue |
| 94 | + rLinuxEc2: |
| 95 | + Type: AWS::EC2::Instance |
| 96 | + Metadata: |
| 97 | + guard: |
| 98 | + SuppressedRules: |
| 99 | + - 'EC2_INSTANCE_DETAILED_MONITORING_ENABLED' |
| 100 | + Properties: |
| 101 | + ImageId: !Ref pOperatingSystem |
| 102 | + IamInstanceProfile: !Ref rec2InstanceProfile |
| 103 | + Monitoring: false |
| 104 | + InstanceType: !Ref pInstanceType |
| 105 | + EbsOptimized: true |
| 106 | + SourceDestCheck: true |
| 107 | + SubnetId: !Ref pSubnet |
| 108 | + SecurityGroupIds: |
| 109 | + - !Ref rSecurityGroupDefault |
| 110 | + BlockDeviceMappings: |
| 111 | + - DeviceName: "/dev/xvda" |
| 112 | + Ebs: |
| 113 | + VolumeSize: !Ref pVolumeSize |
| 114 | + DeleteOnTermination: !Ref pEbsDeleteOnTermination |
| 115 | + Tags: |
| 116 | + - Key: Name |
| 117 | + Value: !Ref pTagNameValue |
| 118 | + UserData: |
| 119 | + Fn::Base64: |
| 120 | + yum update -y |
| 121 | + ## Instance Profiles |
| 122 | + ## EC2 IAM Roles |
| 123 | + rEc2Role: |
| 124 | + Type: AWS::IAM::Role |
| 125 | + Properties: |
| 126 | + RoleName: !Sub "ec2-role-${AWS::StackName}" |
| 127 | + AssumeRolePolicyDocument: |
| 128 | + Statement: |
| 129 | + - Effect: Allow |
| 130 | + Principal: |
| 131 | + Service: [ec2.amazonaws.com] |
| 132 | + Action: ['sts:AssumeRole'] |
| 133 | + Path: / |
| 134 | + ManagedPolicyArns: |
| 135 | + - !Sub 'arn:${AWS::Partition}:iam::aws:policy/AmazonSSMManagedInstanceCore' |
| 136 | + - !Sub 'arn:${AWS::Partition}:iam::aws:policy/CloudWatchAgentServerPolicy' |
| 137 | + rec2InstanceProfile: |
| 138 | + Type: AWS::IAM::InstanceProfile |
| 139 | + Properties: |
| 140 | + InstanceProfileName: !Sub "ec2-profile-${AWS::StackName}" |
| 141 | + Path: / |
| 142 | + Roles: |
| 143 | + - !Ref rEc2Role |
| 144 | +Outputs: |
| 145 | + oLinuxEc2InstanceId: |
| 146 | + Description: Resource ID of the newly created EC2 instance |
| 147 | + Value: !Ref rLinuxEc2 |
| 148 | + oLinuxEc2PrivateIP: |
| 149 | + Description: Private IP Address for EC2 |
| 150 | + Value: !GetAtt rLinuxEc2.PrivateIp |
0 commit comments