Skip to content

Commit cfe6305

Browse files
authored
Merge pull request #2333 from biswanathmukherjee/biswanathmukherjee-feature-apigw-lambda-cognito-v2
New Serverless Pattern - Amazon Cognito user management using Amazon API Gateway and AWS Lambda
2 parents 0f4b0e1 + 6a72e0a commit cfe6305

File tree

12 files changed

+873
-0
lines changed

12 files changed

+873
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# Amazon Cognito user management using Amazon API Gateway and AWS Lambda
2+
3+
This sample project deploys an Amazon Cognitor user pool, an Amazon API Gateway REST API with an AWS Lambda integration. The Lambda function, written in Java, calls the Amazon Cognito API to create and confirm a user. The project also exposes API to return tokens after successful login by the user.
4+
5+
Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/apigw-lambda-cognito-sam-java
6+
7+
Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example.
8+
9+
## Requirements
10+
11+
- [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources.
12+
- [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured
13+
- [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
14+
- [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed
15+
- [Java 21 or above](https://docs.aws.amazon.com/corretto/latest/corretto-21-ug/downloads-list.html) installed
16+
- [Maven 3.8.6 or above](https://maven.apache.org/download.cgi) installed
17+
18+
19+
20+
## Deployment Instructions
21+
22+
1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
23+
```bash
24+
git clone https://github.com/aws-samples/serverless-patterns
25+
```
26+
27+
2. Change directory to the pattern directory:
28+
```bash
29+
cd serverless-patterns/apigw-lambda-cognito-sam-java
30+
```
31+
32+
3. From the command line, execute the below command to build the Java based AWS Lambda function.
33+
```bash
34+
sam build
35+
```
36+
37+
4. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file:
38+
```bash
39+
sam deploy --guided
40+
```
41+
4. During the prompts:
42+
43+
- Enter a stack name
44+
- Enter the desired AWS Region.
45+
- Allow SAM CLI to create IAM roles with the required permissions.
46+
- MyCreateUserFunction has no authentication. Is this okay? [y/N]: select `y`
47+
- MyConfirmUserFunction has no authentication. Is this okay? [y/N]: select `y`
48+
- MyLoginUserFunction has no authentication. Is this okay? [y/N]: select `y`
49+
50+
51+
For production applications, you should [enable authentication for the API Gateway](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-control-access-to-api.html) using one of several available options and [follow the API Gateway security best practices](https://docs.aws.amazon.com/apigateway/latest/developerguide/security-best-practices.html).
52+
53+
- Keep default values to the rest of the parameters.
54+
55+
Once you have run `sam deploy --guided` mode once and saved arguments to a configuration file (samconfig.toml), you can use `sam deploy` in future to use these defaults.
56+
57+
5. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for next step as well as testing.
58+
59+
## How it works
60+
61+
Please refer to the architecture diagram below:
62+
63+
![End to End Architecture](images/architecture.png)
64+
65+
Here's a breakdown of the steps:
66+
67+
1. **Amazon API Gateway**: Receives the HTTP POST request containing the request details in JSON format. There are 3 POST methods exposed for create user, confirm user and login user operations.
68+
69+
2. **AWS Lambda**: Triggered by the API Gateway, this function written in Java uses Amazon Cognito API to create, confirm and login user.
70+
71+
3. **Amazon Cognito**: Depending on the operation invoked, user is created, confirmed or logged in the Amazon Cognito user pool.
72+
73+
4. **Response**: The Lambda function processes the Amazon Cognito API response and sends it back to the user via the API Gateway.
74+
75+
## Testing
76+
77+
1. You can use [curl](https://curl.se/) or any other tool of your choice to send a HTTP POST create user request to the API. Make sure to replace `CreateUserAPIGatewayEndpoint` with the corresponding values from your `sam deploy --guided` output. Replace `YourFirstName`, `YourLastName`, `your-email` and `your-password` with your values. The `email` should be a valid email id.
78+
79+
```bash
80+
curl -d '{"firstName": "{YourFirstName}", "lastName": "{YourLastName}", "email": "{your-email}", "password": "{your-password}"}' -H 'Content-Type: application/json' {CreateUserAPIGatewayEndpoint}
81+
```
82+
83+
The API returns a response as below:
84+
85+
```json
86+
{"isSuccessful":true,"statusCode":200,"cognitoUserId":"84c8xxxx-xxxx-xxxx-xxx-xxxxx9846de1","isConfirmed":false}
87+
```
88+
89+
2. You should receive an email with a verification code at your provided email id. Use the verification code to invoke the confirm user POST API:
90+
```bash
91+
curl -d '{"email": "{your-email@mail.com}", "code": "{VerificationCode}"}' -H 'Content-Type: application/json' {ConfirmUserAPIGatewayEndpoint}
92+
```
93+
94+
The API returns a response as below:
95+
96+
```json
97+
{"isSuccessful":true,"statusCode":200}
98+
```
99+
100+
3. Now, you can call the login POST API. This API will return the token:
101+
```bash
102+
curl -d '{"email": "{your-email@mail.com}", "password": "{your-password}"}' -H 'Content-Type: application/json' {LoginUserAPIGatewayEndpoint}
103+
```
104+
105+
The API returns a response as below:
106+
107+
```json
108+
{"isSuccessful":true, "statusCode":200, "idToken":"eyJra...", "accessToken": "eyJra...", "refreshToken": "eyJj..."}
109+
```
110+
111+
## Cleanup
112+
113+
1. To delete the resources deployed to your AWS account via AWS SAM, run the following command:
114+
115+
```bash
116+
sam delete
117+
```
118+
119+
120+
---
121+
122+
Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
123+
124+
SPDX-License-Identifier: MIT-0
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
{
2+
"title": "Amazon Cognito user management using Amazon API Gateway and AWS Lambda",
3+
"description": "Deploys Amazon Cognito user pool, Amazon API Gateway REST API with AWS Lambda integration for user creation, confirmation & token retrieval in Java.",
4+
"language": "Java",
5+
"level": "200",
6+
"framework": "SAM",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"Amazon API Gateway receives the HTTP POST request containing the request details in JSON format. There are 3 POST methods exposed for create user, confirm user and login user operations.",
11+
"AWS Lambda is by the API Gateway, this function written in Java uses Amazon Cognito API to create, confirm and login user.",
12+
"Depending on the operation invoked, user is created, confirmed or logged in the Amazon Cognito user pool.",
13+
"The Lambda function processes the Amazon Cognito API response and sends it back to the user via the API Gateway."
14+
]
15+
},
16+
"gitHub": {
17+
"template": {
18+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/apigw-lambda-cognito-sam-java",
19+
"templateURL": "serverless-patterns/apigw-lambda-cognito-sam-java",
20+
"projectFolder": "apigw-lambda-cognito-sam-java",
21+
"templateFile": "template.yaml"
22+
}
23+
},
24+
"resources": {
25+
"bullets": [
26+
{
27+
"text": "Call a REST API integrated with an Amazon Cognito user pool",
28+
"link": "https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-invoke-api-integrated-with-cognito-user-pool.html"
29+
},
30+
{
31+
"text": "Building fine-grained authorization using Amazon Cognito, API Gateway, and IAM",
32+
"link": "https://aws.amazon.com/blogs/security/building-fine-grained-authorization-using-amazon-cognito-api-gateway-and-iam/"
33+
}
34+
]
35+
},
36+
"deploy": {
37+
"text": [
38+
"sam build",
39+
"sam deploy --guided"
40+
]
41+
},
42+
"testing": {
43+
"text": [
44+
"See the GitHub repo for detailed testing instructions."
45+
]
46+
},
47+
"cleanup": {
48+
"text": [
49+
"Delete the stack: <code>sam delete</code>."
50+
]
51+
},
52+
"authors": [
53+
{
54+
"name": "Biswanath Mukherjee",
55+
"image": "https://d1rwvjey2iif32.cloudfront.net",
56+
"bio": "I am a Sr. Solutions Architect working at AWS India.",
57+
"linkedin": "biswanathmukherjee"
58+
}
59+
],
60+
"patternArch": {
61+
"icon1": {
62+
"x": 20,
63+
"y": 50,
64+
"service": "apigw",
65+
"label": "API Gateway"
66+
},
67+
"icon2": {
68+
"x": 50,
69+
"y": 50,
70+
"service": "lambda",
71+
"label": "AWS Lambda"
72+
},
73+
"icon3": {
74+
"x": 80,
75+
"y": 50,
76+
"service": "cognito",
77+
"label": "Cognito"
78+
},
79+
"line1": {
80+
"from": "icon1",
81+
"to": "icon2",
82+
"label": ""
83+
},
84+
"line2": {
85+
"from": "icon2",
86+
"to": "icon3",
87+
"label": ""
88+
}
89+
}
90+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{
2+
"title": "Amazon Cognito user management using Amazon API Gateway and AWS Lambda",
3+
"description": "Deploys Amazon Cognito user pool, Amazon API Gateway REST API with AWS Lambda integration for user creation, confirmation & token retrieval in Java.",
4+
"language": "Java",
5+
"level": "200",
6+
"framework": "SAM",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"Amazon API Gateway receives the HTTP POST request containing the request details in JSON format. There are 3 POST methods exposed for create user, confirm user and login user operations.",
11+
"AWS Lambda is by the API Gateway, this function written in Java uses Amazon Cognito API to create, confirm and login user.",
12+
"Depending on the operation invoked, user is created, confirmed or logged in the Amazon Cognito user pool.",
13+
"The Lambda function processes the Amazon Cognito API response and sends it back to the user via the API Gateway."
14+
]
15+
},
16+
"gitHub": {
17+
"template": {
18+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/apigw-lambda-cognito-sam-java",
19+
"templateURL": "serverless-patterns/apigw-lambda-cognito-sam-java",
20+
"projectFolder": "apigw-lambda-cognito-sam-java",
21+
"templateFile": "template.yaml"
22+
}
23+
},
24+
"resources": {
25+
"bullets": [
26+
{
27+
"text": "Call a REST API integrated with an Amazon Cognito user pool",
28+
"link": "https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-invoke-api-integrated-with-cognito-user-pool.html"
29+
},
30+
{
31+
"text": "Building fine-grained authorization using Amazon Cognito, API Gateway, and IAM",
32+
"link": "https://aws.amazon.com/blogs/security/building-fine-grained-authorization-using-amazon-cognito-api-gateway-and-iam/"
33+
}
34+
]
35+
},
36+
"deploy": {
37+
"text": [
38+
"sam build",
39+
"sam deploy --guided"
40+
]
41+
},
42+
"testing": {
43+
"text": [
44+
"See the GitHub repo for detailed testing instructions."
45+
]
46+
},
47+
"cleanup": {
48+
"text": [
49+
"Delete the stack: <code>sam delete</code>."
50+
]
51+
},
52+
"authors": [
53+
{
54+
"name": "Biswanath Mukherjee",
55+
"image": "https://d1rwvjey2iif32.cloudfront.net",
56+
"bio": "I am a Sr. Solutions Architect working at AWS India.",
57+
"linkedin": "biswanathmukherjee"
58+
}
59+
]
60+
}
17.5 KB
Loading
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>com.example</groupId>
7+
<artifactId>apigw-lambda-cognito</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
<name>Cognito user management</name>
11+
<properties>
12+
<maven.compiler.source>21</maven.compiler.source>
13+
<maven.compiler.target>21</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencyManagement>
18+
<dependencies>
19+
<dependency>
20+
<groupId>software.amazon.awssdk</groupId>
21+
<artifactId>bom</artifactId>
22+
<version>2.26.20</version>
23+
<type>pom</type>
24+
<scope>import</scope>
25+
</dependency>
26+
</dependencies>
27+
</dependencyManagement>
28+
29+
<dependencies>
30+
<dependency>
31+
<groupId>com.amazonaws</groupId>
32+
<artifactId>aws-lambda-java-core</artifactId>
33+
<version>1.2.3</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>com.amazonaws</groupId>
37+
<artifactId>aws-lambda-java-events</artifactId>
38+
<version>3.11.3</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>software.amazon.awssdk</groupId>
42+
<artifactId>aws-core</artifactId>
43+
</dependency>
44+
<dependency>
45+
<groupId>software.amazon.lambda</groupId>
46+
<artifactId>powertools-serialization</artifactId>
47+
<version>1.18.0</version>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.json</groupId>
51+
<artifactId>json</artifactId>
52+
<version>20240303</version>
53+
</dependency>
54+
<dependency>
55+
<groupId>software.amazon.awssdk</groupId>
56+
<artifactId>cognitoidentity</artifactId>
57+
</dependency>
58+
<dependency>
59+
<groupId>software.amazon.awssdk</groupId>
60+
<artifactId>cognitoidentityprovider</artifactId>
61+
</dependency>
62+
<dependency>
63+
<groupId>com.google.code.gson</groupId>
64+
<artifactId>gson</artifactId>
65+
<version>2.10.1</version>
66+
</dependency>
67+
</dependencies>
68+
<build>
69+
<plugins>
70+
<plugin>
71+
<groupId>org.apache.maven.plugins</groupId>
72+
<artifactId>maven-surefire-plugin</artifactId>
73+
<version>3.3.1</version>
74+
</plugin>
75+
<plugin>
76+
<groupId>org.apache.maven.plugins</groupId>
77+
<artifactId>maven-shade-plugin</artifactId>
78+
<version>3.6.0</version>
79+
<configuration>
80+
</configuration>
81+
<executions>
82+
<execution>
83+
<phase>package</phase>
84+
<goals>
85+
<goal>shade</goal>
86+
</goals>
87+
</execution>
88+
</executions>
89+
</plugin>
90+
</plugins>
91+
</build>
92+
</project>

0 commit comments

Comments
 (0)