Parameters: UniqueName: Type: String BuildType: Type: String BuildImage: Type: String Resources: SxCInstanceProfile: Type: AWS::IAM::InstanceProfile Properties: Roles: - !Ref SxCAccessRole SxCAccessRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: 2012-10-17 Statement: - Effect: Allow Principal: Service: ec2.amazonaws.com Action: sts:AssumeRole RolePolicies: Type: AWS::IAM::Policy DependsOn: - SxCInstance Properties: PolicyName: SxCAccessPolicy PolicyDocument: Version: 2012-10-17 Statement: - Effect: Allow Action: 's3:ListBucket' Resource: !Sub 'arn:aws:s3:::${UniqueName}' - Effect: Allow Action: 's3:*Object' Resource: !Sub 'arn:aws:s3:::${UniqueName}/*' - Effect: Allow Action: 'ec2:CreateTags' Resource: !Sub 'arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:instance/*' Condition: StringEquals: ec2:ResourceTag/Name: !Ref UniqueName Roles: - !Ref SxCAccessRole SxCInstance: Type: AWS::EC2::Instance Properties: InstanceType: !Ref BuildType ImageId: !Ref BuildImage IamInstanceProfile: !Ref SxCInstanceProfile Tags: - Key: Name Value: !Ref UniqueName UserData: Fn::Base64: !Sub | #!/bin/bash -xe echo '- Updating package list' apt update echo '- Installing AWS tools' apt install -y awscli echo '- Installing snapcraft' apt remove lxd-client -y snap install lxd lxd init --auto snap install snapcraft --classic mkdir -p /tmp/robomaker_snap cd /tmp/robomaker_snap echo '- Retrieving source code from S3' aws s3 cp s3://${UniqueName}/src src/ --recursive aws s3 cp s3://${UniqueName}/snap snap/ --recursive echo '- Snapping souce code' snapcraft --use-lxd echo '- Uploading snap to S3' aws s3 cp *.snap s3://${UniqueName}/ echo '- Exiting user data script' token=$(curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600") instance_id=$(curl -H "X-aws-ec2-metadata-token: $token" -v http://169.254.169.254/latest/meta-data/instance-id) aws ec2 create-tags --resources $instance_id --region ${AWS::Region} --tags Key=Status,Value=COMPLETED Outputs: InstanceId: Value: !Ref SxCInstance