Hello Amazon API Gateway

Next step is to add an Amazon API Gateway in front of your function. Amazon API Gateway will expose a public HTTP endpoint that anyone on the internet can hit with web browser.

You will use AWS Lambda proxy integration mounted to the root of the API. This means that any request to any URL path will be proxied directly to your AWS Lambda function, and the response from the function will be returned back to the user.

Update project

First install Amazon.CDK.AWS.APIGateway Nuget package.

API Gateway Nuget

Add the following using statement:

using Amazon.CDK.AWS.APIGateway;

Next add a LambdaRestApi construct to your stack that defines an API endpoint and associate it with our AWS Lambda function.

namespace CdkLab
    public class CdkLabStack : Stack
        internal CdkLabStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props)

            // defines an API Gateway REST API resource backed by our "hello" function.
            new LambdaRestApi(this, "Endpoint", new LambdaRestApiProps
                Handler = hello

That’s it. This is all you need to do in order to define an Amazon API Gateway which proxies all requests to an AWS Lambda function.


First run cdk diff to take a look at the changes that are going to be deployed.

cdk diff

If you scroll down the output, you will see that 12 new resources will be created and one endpoint will be output:

[+] AWS::ApiGateway::RestApi Endpoint EndpointEEF1FD8F
[+] AWS::IAM::Role Endpoint/CloudWatchRole EndpointCloudWatchRoleC3C64E0F
[+] AWS::ApiGateway::Account Endpoint/Account EndpointAccountB8304247
[+] AWS::ApiGateway::Deployment Endpoint/Deployment EndpointDeployment318525DA16a15d5607e49166a2b5ac8009d3fcf3
[+] AWS::ApiGateway::Stage Endpoint/DeploymentStage.prod EndpointDeploymentStageprodB78BEEA0
[+] AWS::ApiGateway::Resource Endpoint/Default/{proxy+} Endpointproxy39E2174E
[+] AWS::Lambda::Permission Endpoint/Default/{proxy+}/ANY/ApiPermission.CdkLabStackEndpointEC285F4D.ANY..{proxy+} EndpointproxyANYApiPermissionCdkLabStackEndpointEC285F4DANYproxy1AC9707C
[+] AWS::Lambda::Permission Endpoint/Default/{proxy+}/ANY/ApiPermission.Test.CdkLabStackEndpointEC285F4D.ANY..{proxy+} EndpointproxyANYApiPermissionTestCdkLabStackEndpointEC285F4DANYproxyC09FEA59
[+] AWS::ApiGateway::Method Endpoint/Default/{proxy+}/ANY EndpointproxyANYC09721C5
[+] AWS::Lambda::Permission Endpoint/Default/ANY/ApiPermission.CdkLabStackEndpointEC285F4D.ANY.. EndpointANYApiPermissionCdkLabStackEndpointEC285F4DANYC38198EC
[+] AWS::Lambda::Permission Endpoint/Default/ANY/ApiPermission.Test.CdkLabStackEndpointEC285F4D.ANY.. EndpointANYApiPermissionTestCdkLabStackEndpointEC285F4DANYA08D7609
[+] AWS::ApiGateway::Method Endpoint/Default/ANY EndpointANY485C938B

[+] Output Endpoint/Endpoint Endpoint8024A810: {"Value":{"Fn::Join":["",["https://",{"Ref":"EndpointEEF1FD8F"},".execute-api.",{"Ref":"AWS::Region"},".",{"Ref":"AWS::URLSuffix"},"/",{"Ref":"EndpointDeploymentStageprodB78BEEA0"},"/"]]}}

Now it’s time to deploy Amazon API Gateway.

cdk deploy

When deployment is complete, you’ll notice this line (scroll to the end of output):

 14/14 | 6:26:09 AM | UPDATE_COMPLETE_CLEA | AWS::CloudFormation::Stack  | CdkLabStack
 14/14 | 6:26:09 AM | UPDATE_COMPLETE      | AWS::CloudFormation::Stack  | CdkLabStack

 ✅  CdkLabStack

CdkLabStack.Endpoint8024A810 = https://518w654s91.execute-api.eu-west-1.amazonaws.com/prod/

This is a stack output that’s automatically added by the Amazon API Gateway construct and includes the URL of the Amazon API Gateway endpoint.

Now you can try to open this URL in your browser:

API Gateway test