Easy guide to secure your EC2 using Amazon Cognito
To secure your EC2 instance using Amazon Cognito, you can follow these steps:
- Create a new Cognito user pool to store user information and manage authentication.
- Create an app client in the user pool, which will allow users to interact with the pool and authenticate themselves.
- Create an IAM role that allows access to your EC2 instance, and attach this role to the user pool.
- In your EC2 instance, install and configure the Cognito Identity SDK, which will allow users to authenticate themselves and obtain temporary credentials to access the EC2 instance.
- In your application code, use the SDK to authenticate users and obtain temporary credentials, and then use these credentials to access the EC2 instance.
- To secure your instance further you can also use security group to restrict the inbound and outbound traffic to specific IP or port.
- To monitor the usage and access logs of your instance, you can use CloudTrail and CloudWatch to get a detailed view of the actions that are performed on your EC2 instance.
Please note that this is a high-level overview of the process and you may need to adjust these steps based on your specific use case and requirements.

Here is an example of how you can use the AWS CLI (Command Line Interface) to create an Amazon Cognito user pool, an app client, and an IAM role:
- To create a user pool, you can use the
create-user-pool
command. For example:
aws cognito-idp create-user-pool --pool-name MyUserPool
- To create an app client, you can use the
create-user-pool-client
command. For example:
aws cognito-idp create-user-pool-client --user-pool-id <user-pool-id> --client-name MyAppClient
- To create an IAM role, you can use the
create-role
command. For example:
aws iam create-role --role-name MyEC2Role --assume-role-policy-document '{"Version": "2012-10-17","Statement": [{"Effect": "Allow","Principal": {"Federated": "cognito-identity.amazonaws.com"},"Action": "sts:AssumeRoleWithWebIdentity","Condition": {"StringEquals": {"cognito-identity.amazonaws.com:aud": <user-pool-id>}}}]}'
- To attach a role to the user pool, you can use the
set-identity-pool-roles
command
aws cognito-identity set-identity-pool-roles --identity-pool-id <identity-pool-id> --roles authenticated='<arn-of-authenticated-role>' unauthenticated='<arn-of-unauthenticated-role>'
- To install and configure the Cognito Identity SDK on your EC2 instance, you will need to use the appropriate package manager for your operating system (e.g. apt-get for Ubuntu, yum for Amazon Linux, etc.).
- In your application code, you can use the SDK to authenticate users and obtain temporary credentials.
Please note that the above commands are just examples and you will need to replace the placeholders (e.g. <user-pool-id>
, <identity-pool-id>
, etc.) with the actual values for your environment. Also, you may need to configure your AWS CLI credentials before you can use these commands.
Happy using!
Reza Rezvani — Jan. 2023, Berlin