Skip to content

Commit a8d042a

Browse files
authored
Merge pull request #2487 from klmuthu/klmuthu-feature-apigw-lambda-redshiftdataapi
New serverless pattern - API Gateway - Lambda - Redshift Data API
2 parents d4d8214 + 667ca50 commit a8d042a

File tree

6 files changed

+474
-0
lines changed

6 files changed

+474
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
*.swp
2+
package-lock.json
3+
__pycache__
4+
.pytest_cache
5+
.venv
6+
*.egg-info
7+
8+
# CDK asset staging directory
9+
.cdk.staging
10+
cdk.out
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Amazon Redshift Data API with Amazon API Gateway
2+
3+
This pattern demonstrates how to expose data from Redshift through API using API Gateway, Lambda and Redshift Data API.
4+
5+
Learn more about this pattern at [Serverless Land Patterns](https://serverlessland.com/patterns/apigw-lambda-redshiftdataapi)
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 CDK installed](https://docs.aws.amazon.com/cdk/latest/guide/cli.html)
15+
- [Python 3 installed](https://www.python.org/downloads/)
16+
- [pipenv installed](https://pipenv.pypa.io/en/latest/installation.html)
17+
- [Create Redshift Serverless](https://docs.aws.amazon.com/redshift/latest/gsg/new-user-serverless.html#serverless-console-resource-creation)
18+
19+
## Deployment Instructions
20+
21+
1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
22+
```
23+
git clone https://github.com/aws-samples/serverless-patterns
24+
```
25+
1. Change directory to the pattern directory:
26+
```
27+
cd apigw-lambda-redshiftdataapi
28+
```
29+
1. Create python virtual environment and install dependencies:
30+
```
31+
python3 -m venv venv
32+
source ./venv/bin/activate
33+
pipenv install
34+
pipenv install -r requirements.txt
35+
```
36+
1. Create .env file to include Redshift cluster environment variables:
37+
```
38+
Create a .env file in the cloned directory
39+
Add the following variables with respective to values
40+
REDSHIFT_CLUSTER_ARN=arn:aws:redshift-serverless:<region>:<accountid>:workgroup/<workgroupid>
41+
REDSHIFT_WORKGROUP=<workgroup-name>
42+
REDSHIFT_DATABASE=<database-name>
43+
```
44+
1. From the command line, use AWS CDK to deploy the AWS resources for the pattern as specified in the template.yml file:
45+
46+
```
47+
cdk deploy --app "python3 apigw_lambda_redshiftdataapi_stack.py"
48+
```
49+
50+
1. Note the outputs from the CDK deployment process. These contain the resource names and/or ARNs which are used for testing.
51+
52+
## How it works
53+
54+
This setup orchestrates exposing data from Redshift through API Gateway with Cognito authorizer and Lambda using Redshift Data API.
55+
56+
## Testing
57+
58+
1. Use an api client with oauth capability such as Postman or Rapid API to test the api. API url can be found from CDK stack deployment output ApigwRedshiftDataApi.ApiUrl. You will need to login to AWS Console, navigate to Cognito app client and retrieve the client id and secret to pass it in the api call.
59+
60+
## Cleanup
61+
62+
cdk destroy --app "python3 apigw_lambda_redshiftdataapi_stack.py"
63+
64+
---
65+
66+
Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
67+
68+
SPDX-License-Identifier: MIT-0
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
{
2+
"title": "Amazon Reshift Data API with Amazon API Gateway and AWS Lambda",
3+
"description": "Access data from Redshift with API Gateway, Lambda and Redshift Data API",
4+
"language": "Python",
5+
"level": "200",
6+
"framework": "CDK",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"This sample project demonstrates how to expose data from Redshift through API using API Gateway, Lambda and Redshift Data API.",
11+
"Implemented in CDK."
12+
]
13+
},
14+
"gitHub": {
15+
"template": {
16+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/apigw-lambda-redshiftdataapi",
17+
"templateURL": "serverless-patterns/apigw-lambda-redshiftdataapi",
18+
"projectFolder": "apigw-lambda-redshiftdataapi",
19+
"templateFile": "apigw_lambda_redshiftdataapi_stack.py"
20+
}
21+
},
22+
"resources": {
23+
"bullets": [
24+
{
25+
"text": "Redshift Data API",
26+
"link": "https://docs.aws.amazon.com/redshift/latest/mgmt/data-api.html"
27+
},
28+
{
29+
"text": "API Gateway",
30+
"link": "https://docs.aws.amazon.com/apigateway/latest/developerguide/welcome.html"
31+
}
32+
]
33+
},
34+
"deploy": {
35+
"text": [
36+
"See the GitHub repo for detailed deployment instructions."
37+
]
38+
},
39+
"testing": {
40+
"text": [
41+
"See the GitHub repo for detailed testing instructions."
42+
]
43+
},
44+
"cleanup": {
45+
"text": [
46+
"Delete the stack: <code>cdk deploy --app \"python3 apigw_lambda_redshiftdataapi_stack.py\"</code>."
47+
]
48+
},
49+
"authors": [
50+
{
51+
"name": "Muthu Kumar",
52+
"image": "https://avatars.githubusercontent.com/u/51725180",
53+
"bio": "AWS Solutions Architect. Serverless advocate and enthusiast.",
54+
"linkedin": "klmuthu"
55+
}
56+
],
57+
"patternArch": {
58+
"icon1": {
59+
"x": 20,
60+
"y": 50,
61+
"service": "apigw",
62+
"label": "API Gateway REST API"
63+
},
64+
"icon2": {
65+
"x": 50,
66+
"y": 50,
67+
"service": "lambda",
68+
"label": "AWS Lambda"
69+
},
70+
"icon3": {
71+
"x": 80,
72+
"y": 50,
73+
"service": "redshift",
74+
"label": "Amazon Redshift"
75+
},
76+
"line1": {
77+
"from": "icon1",
78+
"to": "icon2",
79+
"label": "Request"
80+
},
81+
"line2": {
82+
"from": "icon2",
83+
"to": "icon3",
84+
"label": "Redshift Data API"
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)