REST API
Add an API Gateway REST API
We will add an API Gateway REST API with a one endpoint:
POST /dear-santa { gift: "lego" | "surprise", legoSKU?: "40499", from: "emmet" }Update CDK stack
Edit the lib/eda-workshop-stack.ts file to look like this - adding your user name to the API name to identify it:
import { Construct, Stack, StackProps } from "aws-cdk-lib";
import { RestApi } from "aws-cdk-lib/aws-apigateway";
export class EDAWorkshopStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const api = new RestApi(this, "EDAWorkshopAPI-YOUR_USER_NAME");
const dearSanta = api.root.addResource("dear-santa");
dearSanta.addMethod("POST");
}
}Run a build
Take a look at the JavaScript files that get created:
Synthesise the CloudFormation template
Take a look at the cdk.out/EDAWorkshopStack.template.json file that gets created, this is the CloudFormation template that will be deployed:
Bonus: Add a simple test for your REST API to the eda-workshop.test.ts file and run npm test to execute it
Deploy CDK app
Now you can deploy your new CDK app (complete with an API!) to AWS - whenever you want to deploy your app just run this command and it will build and synthesise for you:
Test API endpoint
Once the CDK app has been deployed, you should see an API Gateway URL in the terminal. Use it to test out your API response:
You should get an "internal server error" - this is because we haven't got anything behind the endpoint yet.
So... let's put something behind the endpoint!
Bonus: Enable AWS X-Ray tracing for your REST API
Last updated