Skip to content

Instantly share code, notes, and snippets.

@nnsnodnb
Last active January 31, 2024 12:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nnsnodnb/9a5346c8561208e45ff5177768ef5682 to your computer and use it in GitHub Desktop.
Save nnsnodnb/9a5346c8561208e45ff5177768ef5682 to your computer and use it in GitHub Desktop.
[WIP] CloudFormation template for EC2 Mac Instance bundle latest Xcode.
AWSTemplateFormatVersion: 2010-09-09
Parameters:
AMIId:
ConstraintDescription: Please choose AMIId
Description: Base image id for macOS
Type: AWS::EC2::Image::Id
KeyName:
ConstraintDescription: Must be the name of an existing EC2 KeyPair
Type: AWS::EC2::KeyPair::KeyName
Resources:
DefaultPublicRoute:
DependsOn: VPCGatewayAttachment
Properties:
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref 'InternetGateway'
RouteTableId: !Ref 'PublicRouteTable'
Type: AWS::EC2::Route
InstanceSecurityGroup:
Properties:
GroupDescription: Enable SSH access only
GroupName: instance-sg
SecurityGroupIngress:
- CidrIp: 0.0.0.0/0
FromPort: 22
IpProtocol: tcp
ToPort: 22
VpcId: !Ref 'VPC'
Type: AWS::EC2::SecurityGroup
InternetGateway:
Type: AWS::EC2::InternetGateway
PublicRouteTable:
Properties:
VpcId: !Ref 'VPC'
Type: AWS::EC2::RouteTable
PublicSubnet:
Properties:
AvailabilityZone: !Select
- 0
- !GetAZs ''
CidrBlock: 10.0.10.0/24
VpcId: !Ref 'VPC'
Type: AWS::EC2::Subnet
PublicSubnetRouteTableAssociation:
Properties:
RouteTableId: !Ref 'PublicRouteTable'
SubnetId: !Ref 'PublicSubnet'
Type: AWS::EC2::SubnetRouteTableAssociation
VPC:
Properties:
CidrBlock: 10.0.0.0/16
EnableDnsHostnames: 'true'
EnableDnsSupport: 'true'
Type: AWS::EC2::VPC
VPCGatewayAttachment:
Properties:
InternetGatewayId: !Ref 'InternetGateway'
VpcId: !Ref 'VPC'
Type: AWS::EC2::VPCGatewayAttachment
macOSInstance:
CreationPolicy:
ResourceSignal:
Timeout: PT1H
Properties:
ImageId: !Ref 'AMIId'
InstanceType: mac1.metal
KeyName: !Ref 'KeyName'
NetworkInterfaces:
- AssociatePublicIpAddress: 'true'
DeleteOnTermination: 'true'
DeviceIndex: 0
GroupSet:
- !Ref 'InstanceSecurityGroup'
SubnetId: !Ref 'PublicSubnet'
UserData: !Base64
Fn::Sub: |
#!/bin/bash -xe
curl https://github.com/mas-cli/mas/releases/download/v1.7.1/mas.pkg --output mas.pkg
installer -pkg mas.pkg -target /
mas install 497799835
xcode-select -s /Applications/Xcode.app/Content/Developer
xcode-select --install
sleep 1
osascript << EOD
tell application "System Events"
tell process "Install Command Line Developer Tools"
keystroke return
click button "Agree" of window "License Agreement"
end tell
end tell
EOD
/usr/bin/pip3 install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-py3-latest.tar.gz
/usr/local/bin/cfn-signal -e $? --stack '${AWS::StackName}' --resource 'macOSInstance' --region '${AWS::Region}'
Type: AWS::EC2::Instance
AWSTemplateFormatVersion: 2010-09-09
Parameters:
AMIId:
ConstraintDescription: Please choose AMIId
Description: Base image id for macOS
Type: AWS::EC2::Image::Id
KeyName:
ConstraintDescription: Must be the name of an existing EC2 KeyPair
Type: AWS::EC2::KeyPair::KeyName
Resources:
DefaultPublicRoute:
DependsOn: VPCGatewayAttachment
Properties:
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref 'InternetGateway'
RouteTableId: !Ref 'PublicRouteTable'
Type: AWS::EC2::Route
InstanceSecurityGroup:
Properties:
GroupDescription: Enable SSH access only
GroupName: instance-sg
SecurityGroupIngress:
- CidrIp: 0.0.0.0/0
FromPort: 22
IpProtocol: tcp
ToPort: 22
VpcId: !Ref 'VPC'
Type: AWS::EC2::SecurityGroup
InternetGateway:
Type: AWS::EC2::InternetGateway
PublicRouteTable:
Properties:
VpcId: !Ref 'VPC'
Type: AWS::EC2::RouteTable
PublicSubnet:
Properties:
AvailabilityZone: !Select
- 0
- !GetAZs ''
CidrBlock: 10.0.10.0/24
VpcId: !Ref 'VPC'
Type: AWS::EC2::Subnet
PublicSubnetRouteTableAssociation:
Properties:
RouteTableId: !Ref 'PublicRouteTable'
SubnetId: !Ref 'PublicSubnet'
Type: AWS::EC2::SubnetRouteTableAssociation
VPC:
Properties:
CidrBlock: 10.0.0.0/16
EnableDnsHostnames: 'true'
EnableDnsSupport: 'true'
Type: AWS::EC2::VPC
VPCGatewayAttachment:
Properties:
InternetGatewayId: !Ref 'InternetGateway'
VpcId: !Ref 'VPC'
Type: AWS::EC2::VPCGatewayAttachment
macOSInstance:
CreationPolicy:
ResourceSignal:
Timeout: PT1H
Properties:
ImageId: !Ref 'AMIId'
InstanceType: mac1.metal
KeyName: !Ref 'KeyName'
NetworkInterfaces:
- AssociatePublicIpAddress: 'true'
DeleteOnTermination: 'true'
DeviceIndex: 0
GroupSet:
- !Ref 'InstanceSecurityGroup'
SubnetId: !Ref 'PublicSubnet'
UserData: !Base64
Fn::Sub: |
#!/bin/bash -xe
curl https://github.com/mas-cli/mas/releases/download/v1.7.1/mas.pkg --output mas.pkg
installer -pkg mas.pkg -target /
mas install 497799835
xcode-select -s /Applications/Xcode.app/Content/Developer
xcode-select --install
sleep 1
osascript << EOD
tell application "System Events"
tell process "Install Command Line Developer Tools"
keystroke return
click button "Agree" of window "License Agreement"
end tell
end tell
EOD
/usr/bin/pip3 install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-py3-latest.tar.gz
/usr/local/bin/cfn-signal -e $? --stack '${AWS::StackName}' --resource 'macOSInstance' --region '${AWS::Region}'
Type: AWS::EC2::Instance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment