y0zg / macos-aws

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

macOS based EC2 (Infra-as-Code)

Prerequisites

A macOS based EC2 relies on a AWS dedicated host

Key pair

The key pair will be used to access the EC2 instance via SSH

ssh-keygen -P "" -t rsa -b 4096 -m pem -f key -C ""

It will generate a key file (private key) and a key.pub file (public key) in the root folder

Terraform

(Within the terraform folder)

Create AWS assets

Initialize Terraform providers

terraform init

Deploy Terraform resources

terraform apply -var region=us-west-1 -var az=eu-west-1a

The result will be a running macOS based EC2 instance, within a VPC and accesible via SSH (port 22).

ℹ️ Wait until the instance status checks are fine (may take several minutes)

Test SSH connection

ssh -i ../key ec2-user@<Instance-Public-HostName>>

Remove AWS assets (optional)

In case you want to remove all AWS assets

terraform destroy

⚠️ As the EC2 instance stays in terminated state for few hours, you cannot remove the Dedicated Host until the EC2 instance is gone. Therefore, the first time you run the above command, it will fail, but all AWS assets are removed except the Dedicated Host. So, execute the command a second time when the EC2 instance disappears.

Ansible

(Within the ansible folder)

Prerequisites

We are going to use the amazon.aws.aws_ec2 Ansible plugin, in order to build a dynamic Ansible inventory based on tag values (see aws_ec2.yaml file)

pip install boto3 botocore

Test

Run the following command, and it should return your EC2 instance

ansible-inventory --list
ansible all --private-key ../key --user ec2-user --module-name ping

Connect to EC2 with VNC

Execute Ansible playbook in order to set a password for user ec2-user and enable VNC

ansible-playbook --private-key ../key --user ec2-user --extra-vars "password=<password for ec2-user>" configure.yaml

As VNC data is not encrypted, we are going to use VNC through an SSH tunnel

ssh -L 5900:localhost:5900 -i ../key ec2-user@<Instance-Public-HostName>

Starting from now, with a VNC client, the EC2 instance can be reached from vnc://localhost:5900

⚠️ The EC2 instance may be configured with an english keyboard layout

Resources

About


Languages

Language:HCL 100.0%