Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding apigw-sfn-crud-terraform pattern #2444

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 139 additions & 0 deletions apigw-sfn-crud-terraform/Postman.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
{
"info": {
"_postman_id": "1045b2bc-58ae-4a8d-9f0b-35d5cb44e266",
"name": "Random",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "8276188"
},
"variable": [
{
"key": "endpoint",
"value": "<your endpoint here>",
"type": "string"
},
{
"key": "id",
"value": "<your id>",
"type": "string"
}
],
"item": [
{
"name": "DDB Read All",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "{{endpoint}}",
"host": [
"{{endpoint}}"
]
}
},
"response": []
},
{
"name": "DDB Read One",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "{{endpoint}}/{{id}}",
"host": [
"{{endpoint}}"
],
"path": [
"{{id}}"
]
}
},
"response": []
},
{
"name": "DDB Delete",
"request": {
"method": "DELETE",
"header": [],
"url": {
"raw": "{{endpoint}}/{{id}}",
"host": [
"{{endpoint}}"
],
"path": [
"{{id}}"
]
}
},
"response": []
},
{
"name": "DDB Create",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\n \"make\":\"MINI\",\n \"model\": \"Countryman\",\n \"year\": 2015,\n \"trim\": \"JCW All Four\"\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{endpoint}}",
"host": [
"{{endpoint}}"
]
}
},
"response": []
},
{
"name": "DDB Update",
"request": {
"method": "PUT",
"header": [],
"body": {
"mode": "raw",
"raw": "{\"message\":\"my message again from postman\"}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{endpoint}}/{{id}}",
"host": [
"{{endpoint}}"
],
"path": [
"{{id}}"
]
}
},
"response": []
}
],
"event": [
{
"listen": "prerequest",
"script": {
"type": "text/javascript",
"exec": [
""
]
}
},
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
""
]
}
}
]
}
67 changes: 67 additions & 0 deletions apigw-sfn-crud-terraform/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Amazon API Gateway, AWS Step Functions, to Amazon DynamoDB CRUD API

This stack creates a fully functioning CRUD API powered by Amazon API Gateway direct integration to AWS Step Functions and backed by Amazon DynamoDB.

Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/apigw-sfn-crud-terraform

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.

## Requirements

* [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.
* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured
* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
* [Terraform](https://learn.hashicorp.com/tutorials/terraform/install-cli?in=terraform/aws-get-started) installed

## Deployment Instructions

1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
```
git clone https://github.com/aws-samples/serverless-patterns
```
2. Change directory to the pattern directory:
```
cd apigw-sfn-crud-terraform
```
3. From the command line, initialize terraform to download and install the providers defined in the configuration:
```
terraform init
```
4. From the command line, apply the configuration in the main.tf file:
```
terraform apply
```
5. During the prompts:
* Enter yes
6. Note the outputs from the deployment process. These contain the resource names and/or URLs which are used for testing.

## How it works

Amazon API Gateway creates a direct integration with AWS Step Functions utilizing a synchronous call. Step functions evaluates the path and method to choose the proper action. The action steps can be modified to meet your needs.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Amazon API Gateway creates a direct integration with AWS Step Functions utilizing a synchronous call. Step functions evaluates the path and method to choose the proper action. The action steps can be modified to meet your needs.
Amazon API Gateway creates a direct integration with AWS Step Functions utilizing a synchronous call. The Step Functions state machine evaluates the path and method to choose the proper action. The action steps can be modified to meet your needs.


## Testing

*CRUD = Create, Read, Update, Delete*

Once the application is deployed, use a tool like Postman or Curl to call the different CRUD endpoints. If you would like to import the Postman package, update the endpoint and import into Postman.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't clear to me what is meant be updating the endpoint and importing it? Please also add an example of a curl request


## Cleanup
1. Change directory to the pattern directory:
```
cd apigw-sfn-crud-terraform
```
2. Delete all created resources by terraform
```bash
terraform destroy
```
3. During the prompts:
* Enter yes
4. Confirm all created resources has been deleted
```bash
terraform show
```
----
Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: MIT-0

105 changes: 105 additions & 0 deletions apigw-sfn-crud-terraform/crud-asl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
{
"Comment" : "A description of my state machine",
"StartAt" : "Does ID exist?",
"States" : {
"Does ID exist?" : {
"Type" : "Choice",
"Choices" : [ {
"Variable" : "$.pathParam",
"IsPresent" : true,
"Next" : "Check HTTP Method with ID"
} ],
"Default" : "Check HTTP Method"
},
"Check HTTP Method" : {
"Type" : "Choice",
"Choices" : [ {
"Variable" : "$.httpMethod",
"StringMatches" : "POST",
"Next" : "Create Item"
} ],
"Default" : "Scan"
},
"Scan" : {
"Type" : "Task",
"Parameters" : {
"TableName" : "${DDBTable}"
},
"Resource" : "arn:aws:states:::aws-sdk:dynamodb:scan",
"End" : true
},
"Create Item" : {
"Type" : "Task",
"Resource" : "arn:aws:states:::dynamodb:putItem",
"Parameters" : {
"TableName" : "${DDBTable}",
"Item" : {
"id" : {
"S.$" : "States.UUID()"
},
"data" : {
"M.$" : "$.data"
}
}
},
"ResultPath" : null,
"End" : true
},
"Check HTTP Method with ID" : {
"Type" : "Choice",
"Choices" : [ {
"Variable" : "$.httpMethod",
"StringMatches" : "PUT",
"Next" : "Update Item"
}, {
"Variable" : "$.httpMethod",
"StringMatches" : "DELETE",
"Next" : "DynamoDB DeleteItem"
} ],
"Default" : "Get Item"
},
"Get Item" : {
"Type" : "Task",
"Resource" : "arn:aws:states:::dynamodb:getItem",
"Parameters" : {
"TableName" : "${DDBTable}",
"Key" : {
"id" : {
"S.$" : "$.pathParam"
}
}
},
"End" : true
},
"Update Item" : {
"Type" : "Task",
"Resource" : "arn:aws:states:::dynamodb:putItem",
"Parameters" : {
"TableName" : "${DDBTable}",
"Item" : {
"id" : {
"S.$" : "$.pathParam"
},
"data" : {
"M.$" : "$.data"
}
}
},
"ResultPath" : null,
"End" : true
},
"DynamoDB DeleteItem" : {
"Type" : "Task",
"Resource" : "arn:aws:states:::dynamodb:deleteItem",
"Parameters" : {
"TableName" : "${DDBTable}",
"Key" : {
"id" : {
"S.$" : "$.pathParam"
}
}
},
"End" : true
}
}
}
63 changes: 63 additions & 0 deletions apigw-sfn-crud-terraform/example-pattern.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"title": "Amazon API Gateway, AWS Step Functions, to Amazon DynamoDB CRUD API",
"description": "Create a CRUD API with AWS Step Functions.",
"language": "Terraform",
"level": "200",
"framework": "Terraform",
"introBox": {
"headline": "Create a CRUD API with AWS Step Functions.",
"text": [
"This stack creates a fully functioning CRUD API powered by Amazon API Gateway direct integration to AWS Step Functions and backed by Amazon DynamoDB."
]
},
"gitHub": {
"template": {
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/apigw-sfn-crud-terraform",
"templateURL": "serverless-patterns/apigw-sfn-crud-terraform",
"projectFolder": "apigw-sfn-crud-terraform,
"templateFile": "main.tf"
}
},
"resources": {
"bullets": [
{
"text": "Synchronous Express Workflows for AWS Step Functions",
"link": "https://aws.amazon.com/blogs/compute/new-synchronous-express-workflows-for-aws-step-functions"
},
{
"text": "API Gateway VTL",
"link": "https://docs.aws.amazon.com/apigateway/latest/developerguide/rest-api-data-transformations.html"
},
{
"text": "Simple CRUD with API Gateway & DynamboDB",
"link": "https://github.com/aws-samples/serverless-patterns/tree/main/apigw-ddb-openapi-crud"
}
]
},
"deploy": {
"text": [
"terraform init",
"terraform apply"
]
},
"testing": {
"text": [
"See the GitHub repo for detailed testing instructions."
]
},
"cleanup": {
"text": [
"terraform destroy"
]
},
"authors": [
{
"name": "Oriol Matavacas",
"image": "https://togithub.s3.eu-west-1.amazonaws.com/Oriol.jpg",
"bio": "Oriol Matavacas is a Sr. Solutions Architect at AWS based in Barcelona. Oriol primarily supporting customers on the journey to the Cloud. He enjoys building new solutions with scalability, availability and easy to maintain by using serverless.",
"linkedin": "https://www.linkedin.com/in/oriol-matavacas-rodriguez-b165868a",
"twitter": ""
}
]
}

Loading