In this section, we will prepare an EC2 instance running Amazon Linux 2023 as the working environment, connect to it with VS Code Remote - SSH, and use the AWS CLI to run CloudFormation commands throughout the workshop.
Sign in to the AWS Management Console with an IAM user or IAM role that has administrator permissions. Do not use the root user for this workshop.


IaC-Role.

The AdministratorAccess policy is used only to keep this lab simple. In real environments, grant permissions following the principle of least privilege.

cfn-workspace-ec2.
64-bit (x86).
t3.small.
cfn-workspace-key.cfn-workspace-key.pem is downloaded to your computer.

If the public IP address of your network changes, update the SSH inbound rule of this security group to your new My IP.

Choose Actions:

IaC-Role and choose Update IAM role.
cfn-workspace-key.pem file into the ~/.ssh/ folder (on Windows, C:\Users\<user>\.ssh\).
On macOS or Linux, restrict the file permissions with:
chmod 400 ~/.ssh/cfn-workspace-key.pem
Do not share the .pem file or commit it to a repository. AWS does not keep a copy of the private key; if you lose the file, you must create a new key pair.
Open VS Code.
Ctrl + Shift + X).Remote - SSH.Open the Command Palette with Ctrl + Shift + P.
Choose Remote-SSH: Open SSH Configuration File....
Select your user config file (for example ~/.ssh/config).
Add the following configuration, replacing <EC2_Public_IPv4> with the Public IPv4 address
Host cfn-workspace
HostName <EC2_Public_IPv4>
User ec2-user
IdentityFile ~/.ssh/cfn-workspace-key.pem

The Public IPv4 address of the EC2 instance changes every time the instance is stopped and started again. When that happens, update the HostName value.
Remote-SSH: Connect to Host....cfn-workspace.
16. Choose Terminal → New Terminal to open the VS Code terminal.
- Verify that the AWS CLI is preinstalled on Amazon Linux 2023 and is using the IAM role IaC-Role:
aws --version
aws sts get-caller-identity

- The `Arn` value in the output must contain `IaC-Role`.
- Set the default region of the AWS CLI to the region of the EC2 instance:
TOKEN=$(curl -sS -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600")
AWS_REGION=$(curl -sS -H "X-aws-ec2-metadata-token: ${TOKEN}" http://169.254.169.254/latest/meta-data/placement/region)
aws configure set default.region "${AWS_REGION}"
aws configure get region

Install git with dnf:
sudo dnf install -y git
git --version

The day-1, day-2, day-3 folders hold the templates for sections 3, 4, and 5.
cd ~
git clone https://github.com/Hieu192/CloudFormation-series.git
cd CloudFormation-series
ls
