AWSTemplateFormatVersion: '2010-09-09' Description: AWS CloudFormation sample template that contains a single Lambda function behind an API Gateway Resources: GreetingLambda: Type: AWS::Lambda::Function Properties: Code: ZipFile: !Join - "\n" - - '''use strict'';' - '' - // Greeter Lambda - exports.handler = (event, context, callback) => { - ' console.log(''Event:'', JSON.stringify(event));' - ' const name = event.name || ''World'';' - ' const response = {greeting: `Hello, ${name}!`};' - ' callback(null, response);' - '};' Description: A greeting function FunctionName: GreetingLambda Handler: index.handler Role: !GetAtt 'LambdaExecutionRole.Arn' Runtime: nodejs12.x LambdaExecutionRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Principal: Service: - lambda.amazonaws.com Action: - sts:AssumeRole ManagedPolicyArns: - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole GreetingApi: Type: AWS::ApiGateway::RestApi Properties: Name: Greeting API Description: API used for Greeting requests FailOnWarnings: true LambdaPermission: Type: AWS::Lambda::Permission Properties: Action: lambda:invokeFunction FunctionName: !GetAtt 'GreetingLambda.Arn' Principal: apigateway.amazonaws.com SourceArn: !Join - '' - - 'arn:aws:execute-api:' - !Ref 'AWS::Region' - ':' - !Ref 'AWS::AccountId' - ':' - !Ref 'GreetingApi' - /* ApiGatewayCloudWatchLogsRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Principal: Service: - apigateway.amazonaws.com Action: - sts:AssumeRole Policies: - PolicyName: ApiGatewayLogsPolicy PolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Action: - logs:CreateLogGroup - logs:CreateLogStream - logs:DescribeLogGroups - logs:DescribeLogStreams - logs:PutLogEvents - logs:GetLogEvents - logs:FilterLogEvents Resource: '*' ApiGatewayAccount: Type: AWS::ApiGateway::Account Properties: CloudWatchRoleArn: !GetAtt 'ApiGatewayCloudWatchLogsRole.Arn' GreetingApiStage: DependsOn: - ApiGatewayAccount Type: AWS::ApiGateway::Stage Properties: DeploymentId: !Ref 'ApiDeployment' MethodSettings: - DataTraceEnabled: true HttpMethod: '*' LoggingLevel: INFO ResourcePath: /* RestApiId: !Ref 'GreetingApi' StageName: LATEST ApiDeployment: Type: AWS::ApiGateway::Deployment DependsOn: - GreetingRequest Properties: RestApiId: !Ref 'GreetingApi' StageName: DummyStage GreetingResource: Type: AWS::ApiGateway::Resource Properties: RestApiId: !Ref 'GreetingApi' ParentId: !GetAtt 'GreetingApi.RootResourceId' PathPart: greeting GreetingRequest: DependsOn: LambdaPermission Type: AWS::ApiGateway::Method Properties: AuthorizationType: NONE HttpMethod: GET Integration: Type: AWS IntegrationHttpMethod: POST Uri: !Join - '' - - 'arn:aws:apigateway:' - !Ref 'AWS::Region' - :lambda:path/2015-03-31/functions/ - !GetAtt 'GreetingLambda.Arn' - /invocations IntegrationResponses: - StatusCode: '200' RequestTemplates: application/json: !Join - '' - - '{' - ' "name": "$input.params(''name'')"' - '}' RequestParameters: method.request.querystring.name: false ResourceId: !Ref 'GreetingResource' RestApiId: !Ref 'GreetingApi' MethodResponses: - StatusCode: '200' GreetingApiCustomDomain: Type: AWSSamples::ApiGateway::CustomDomain::MODULE Properties: CertificateArn: !Ref CertificateArn CustomDomainName: !Ref CustomDomainName ApiType: EDGE ApiId: !Ref GreetingApi ApiStage: !Ref GreetingApiStage ApiBasePath: '' PrivateHostedZoneId: '' PublicHostedZoneId: !Ref PublicHostedZoneId Parameters: PublicHostedZoneId: Description: The ID of the public Route 53 hosted zone that you want to create records in. Specify at least one Private or Public hosted zone id. Type: String CustomDomainName: Description: Custom domain name for the API Type: String CertificateArn: Description: Arn of ACM certificate matching the custom domain Type: String Outputs: RootUrl: Description: Root URL of the API gateway Value: !Join - '' - - https:// - !Ref 'GreetingApi' - .execute-api. - !Ref 'AWS::Region' - .amazonaws.com