Spin up a Mac EC2 instance on AWS using CloudFormation and GitHub Actions
https://aws.amazon.com/de/ec2/instance-types/mac/
- Fork the repository
- Add AWS credentials to repository secrets
- Run workflow "Deployment" and insert variables
- Instance type: e. g.
mac2.metal(M1 Mac mini) - AMI ID: e. g.
ami-00171e83b9ddc38bd(macOS Sequoia 15.4.1) - Region: for example
eu-west-1 - Availability zone: for example
eu-west-1b - VPC ID: e. g. vpc-0123456789abcdef (should be in the chosen AWS region, e. g.
eu-west-1) - Subnet ID: e. g. subnet-0123456789abcdef (should be in the chosen availability zone, e. g.
eu-west-1b) - SSH key name: e. g.
my-keypair(should already exist, see also Prerequisites below)
Warning
The Mac dedicated host has a minimum allocation time of 24 hours, which means there is a minimum cost of approx. USD 15.60 for the host (depending on the host instance type) once it is running. The host cannot be destroyed before that time. After the 24 hours, the cost is caclulated per minute as usual.
-
Download key pair
From AWS Console, download the key pair used for the EC2 instance to your local machine.
Ensure it has the proper permissions:
chmod 400 my-keypair.pem
-
Connect to the instance:
ssh -i my-keypair.pem ec2-user@<my-instance-hostname>.<region>.compute.amazonaws.com
-
Enable macOS screen sharing
sudo defaults write /var/db/launchd.db/com.apple.launchd/overrides.plist com.apple.screensharing -dict Disabled -bool false sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.screensharing.plist -
Set password for
ec2-usersudo /usr/bin/dscl . -passwd /Users/ec2-user -
Open SSH tunnel
ssh -i my-keypair.pem -L 5900:localhost:5900 ec2-user@<my-instance-hostname>.<region>.compute.amazonaws.com
-
Connect to VNC
On a Mac, for example, press Cmd+K in the Finder and connect to
vnc://localhost:5900.
- Add
AWS_ACCESS_KEY_IDto repository variables. - Add
AWS_SECRET_ACCESS_KEYto repository secrets.
In case you have not set up a VPC yet: AWS Console > VPC > Your VPCs > Actions > Create default VPC
Note
Make sure to create the VPC in the same region as your new EC2 instance.
aws ec2 create-key-pair --key-type rsa --key-name my-keypair --region <my region> --query 'KeyMaterial' --output text > my-keypair.pem
chmod 400 my-keypair.pem