6.2. Setup and configure EC2

In the previous section, we successfully ran the application locally. Now, we will prepare an EC2 server to deploy the application to the cloud environment.

Create IAM role

Search for the IAM service in the console.

6.2.1

Select Roles and click Create role.

6.2.2

When creating the role in the console:

  • Trusted entity type: select AWS service.
  • Use case: EC2, then select EC2.
  • Scroll down and click Next.

6.2.3

For the policy selection, choose the following:

  • For Cognito, search Cognito and select AmazonESCognitoAccess and AmazonCognitoReadOnly.
  • For DynamoDB, search Dynamodb and select AmazonDynamoDBFullAccess and AmazonDynamoDBFullAccess_v2.

6.2.4

Click Next after selecting the policies.

6.2.5

Name the role aws-role-cognito-workshop and verify that all selected policies are correct.

6.2.6

Once everything is set, click Create role.

6.2.7 6.2.8

Setup & run EC2 instance

Search for and select the EC2 service.

6.2.9

Go to Instances and click Launch instances.

6.2.10

Create an EC2 instance with the following details:

  • Name: cognito-workshop-ec2.
  • AMI: select Ubuntu, Ubuntu Server 24.04 LTS (HVM), SSH Volume Type.

6.2.11

Next, choose the instance type t3.small and select a key pair (create one if needed) to SSH into the server.

6.2.12

In Network settings, place the EC2 in a public subnet under the default VPC. Use the default Security Group. Note: for production deployments, review security carefully; here we keep it simple for the lab.

6.2.13

After setting everything up, click Launch instance and wait for it to be ready.

6.2.14

Attach IAM role to EC2 instance

Once the EC2 instance is running, attach the IAM role we created earlier. Why? Locally, we used an Access Key with admin privileges. In the cloud, using an IAM role is safer and serves as a good example.

Select the newly created EC2 instance.

  • Open Actions.
  • Select Instance settings.
  • Click Modify instance metadata options.

6.2.15

Set IMDSv2 to Optional and click Save.

6.2.16 6.2.17

Next, select the EC2 instance again.

6.2.18

Attach the previously created IAM role and click Update IAM role.

6.2.19 6.2.20

The EC2 instance is now ready for deployment.