|
| 1 | +Metadata: |
| 2 | + License: Apache-2.0 |
| 3 | +AWSTemplateFormatVersion: '2010-09-09' |
| 4 | +Description: 'AWS CloudFormation Template to create an EC2 instance |
| 5 | + **WARNING** This template creates an Amazon EC2 instance. |
| 6 | + You will be billed for the AWS resources used if you create a stack from this template.' |
| 7 | + |
| 8 | +Parameters: |
| 9 | + Namespace: |
| 10 | + Type: String |
| 11 | + Description: An environment name that will be prefixed to resource names |
| 12 | + S3Mounts: |
| 13 | + Type: String |
| 14 | + Description: A JSON array of objects with name, bucket, and prefix properties used to mount data |
| 15 | + IamPolicyDocument: |
| 16 | + Type: String |
| 17 | + Description: The IAM policy to be associated with the launched workstation |
| 18 | + EnvironmentInstanceFiles: |
| 19 | + Type: String |
| 20 | + Description: >- |
| 21 | + An S3 URI (starting with "s3://") that specifies the location of files to be copied to |
| 22 | + the environment instance, including any bootstrap scripts |
| 23 | + InstanceType: |
| 24 | + Description: Choose the instance type for this instance. e.g. t2.small |
| 25 | + Type: String |
| 26 | + Default: t2.small |
| 27 | + AllowedValues: [t2.nano, t2.micro, t2.small, t2.medium] |
| 28 | + ConstraintDescription: must be a valid EC2 instance type. |
| 29 | + KeyPair: |
| 30 | + Description: Name of an existing EC2 KeyPair to enable SSH access to the instance. If no key pairs exist, please create one from the button next to the dropdown. Please contact your Administrator if you are unable to create one. |
| 31 | + Type: AWS::EC2::KeyPair::KeyName |
| 32 | + ConstraintDescription: must be the name of an existing EC2 KeyPair. |
| 33 | + AllowedSSHLocation: |
| 34 | + Description: The IP address range that can be used to SSH to the EC2 instances |
| 35 | + Type: String |
| 36 | + MinLength: '9' |
| 37 | + MaxLength: '18' |
| 38 | + Default: 0.0.0.0/0 |
| 39 | + AllowedPattern: (\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/(\d{1,2}) |
| 40 | + ConstraintDescription: must be a valid IP CIDR range of the form x.x.x.x/x. |
| 41 | + LatestAmiId: |
| 42 | + Description: Enter an AMI-Id e.g. ami-12345. The AMI should exist in the same region as the project. Leave the default string to use the latest Canonical Ubuntu 20.04 image. |
| 43 | + Type: String |
| 44 | + Default: '/aws/service/canonical/ubuntu/server/20.04/stable/current/amd64/hvm/ebs-gp2/ami-id' |
| 45 | + |
| 46 | +Conditions: |
| 47 | + IamPolicyEmpty: !Equals [!Ref IamPolicyDocument, '{}'] |
| 48 | + UserProvidedAMI: !Equals [ 'ami', !Select [0, !Split ["-", !Ref LatestAmiId]]] |
| 49 | + |
| 50 | +Resources: |
| 51 | + IAMRole: |
| 52 | + Type: 'AWS::IAM::Role' |
| 53 | + Properties: |
| 54 | + RoleName: !Join ['-', [Ref: Namespace, 'ec2-role']] |
| 55 | + Path: '/' |
| 56 | + AssumeRolePolicyDocument: |
| 57 | + Version: '2012-10-17' |
| 58 | + Statement: |
| 59 | + - Effect: 'Allow' |
| 60 | + Principal: |
| 61 | + Service: |
| 62 | + - 'ec2.amazonaws.com' |
| 63 | + Action: |
| 64 | + - 'sts:AssumeRole' |
| 65 | + Policies: |
| 66 | + - !If |
| 67 | + - IamPolicyEmpty |
| 68 | + - !Ref 'AWS::NoValue' |
| 69 | + - PolicyName: !Join ['-', [Ref: Namespace, 's3-studydata-policy']] |
| 70 | + PolicyDocument: !Ref IamPolicyDocument |
| 71 | + |
| 72 | + InstanceProfile: |
| 73 | + Type: 'AWS::IAM::InstanceProfile' |
| 74 | + Properties: |
| 75 | + InstanceProfileName: !Join ['-', [Ref: Namespace, 'ec2-profile']] |
| 76 | + Path: '/' |
| 77 | + Roles: |
| 78 | + - Ref: IAMRole |
| 79 | + |
| 80 | + EC2Instance: |
| 81 | + Type: AWS::EC2::Instance |
| 82 | + Properties: |
| 83 | + UserData: |
| 84 | + Fn::Base64: !Sub | |
| 85 | + #!/usr/bin/env bash |
| 86 | + exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1 |
| 87 | + apt update |
| 88 | + apt install -y awscli jq |
| 89 | + # Download and execute bootstrap script |
| 90 | + aws s3 cp "${EnvironmentInstanceFiles}/get_bootstrap_ubuntu.sh" "/tmp" |
| 91 | + chmod 500 "/tmp/get_bootstrap_ubuntu.sh" |
| 92 | + /tmp/get_bootstrap_ubuntu.sh "${EnvironmentInstanceFiles}" '${S3Mounts}' |
| 93 | + |
| 94 | + # Signal result to CloudFormation |
| 95 | + /opt/aws/bin/cfn-signal -e $? --stack "${AWS::StackName}" --resource "EC2Instance" --region "${AWS::Region}" |
| 96 | + InstanceType: !Ref 'InstanceType' |
| 97 | + SecurityGroups: [!Ref 'InstanceSecurityGroup'] |
| 98 | + KeyName: !Ref 'KeyPair' |
| 99 | + ImageId: !If [ UserProvidedAMI,!Ref LatestAmiId,'{{resolve:ssm:/aws/service/canonical/ubuntu/server/20.04/stable/current/amd64/hvm/ebs-gp2/ami-id}}'] |
| 100 | + IamInstanceProfile: !Ref InstanceProfile |
| 101 | + PropagateTagsToVolumeOnCreation: true |
| 102 | + Tags: |
| 103 | + - Key: Name |
| 104 | + Value: !Join ['-', [Ref: Namespace, 'ec2-linux']] |
| 105 | + - Key: Description |
| 106 | + Value: EC2 workspace instance |
| 107 | + - Key: cost_resource |
| 108 | + Value: !Sub ${AWS::StackName} |
| 109 | + |
| 110 | + InstanceSecurityGroup: |
| 111 | + Type: AWS::EC2::SecurityGroup |
| 112 | + Properties: |
| 113 | + GroupDescription: Enable SSH access |
| 114 | + SecurityGroupIngress: |
| 115 | + - IpProtocol: tcp |
| 116 | + FromPort: '22' |
| 117 | + ToPort: '22' |
| 118 | + CidrIp: !Ref 'AllowedSSHLocation' |
| 119 | + |
| 120 | +Outputs: |
| 121 | + InstanceId: |
| 122 | + Description: InstanceId of the newly created EC2 instance |
| 123 | + Value: !Ref 'EC2Instance' |
| 124 | + InstanceIPAddress: |
| 125 | + Description: IP address of the newly created EC2 instance |
| 126 | + Value: !GetAtt [EC2Instance, PublicIp] |
| 127 | + InstanceDNSName: |
| 128 | + Description: DNS name of the newly created EC2 instance |
| 129 | + Value: !GetAtt [EC2Instance, PublicDnsName] |
0 commit comments