0
votes

I am working on swagger file, To create the authorizer in aws apigateway.But that time I 'll mention some function and api in swagger file.But it not effecting in the Apigateway.Once I deleted the stack,It will effect the APIgateway.If any changes is did that not effecting the APIgateway.Below I mentioning the template.yml file and swagger file.Please anyone will help to solve this issue.

template.yml file

AWSTemplateFormatVersion: '2010-09-09'

Transform: AWS::Serverless-2016-10-31

Description: TestApi

Resources:

ApiGateway:

Type: AWS::Serverless::Api
Properties:
  StageName: Prod
  DefinitionUri: s3://devdeliforcemumbailambda/swagger-json-testapi.json

DriverDeleteF:

Type: AWS::Serverless::Function
Properties:
  FunctionName: driver_delete_fun
  Handler: index.handler
  Runtime: nodejs8.10
  CodeUri: build/authorizer.zip
  Events:
    GetApi:
      Type: Api
      Properties:
        Path: /driver
        Method: delete

swagger file content

{

"swagger": "2.0",

"info": {

"title": "demo"

}, "host": "rl0cg75uff.execute-api.ap-south-1.amazonaws.com",

"basePath": "/Prod",

"schemes": [

"https"

],

"paths": {

"/driver": {

  "delete": {
    "produces": [
      "application/json"
    ],
    "responses": {
      "200": {
        "description": "200 response",
        "schema": {
          "$ref": "#/definitions/Empty"
        }
      }
    },
    "security": [
      {
        "CognitoAuth": []
       }
    ],
    "x-amazon-apigateway-integration": {
      "uri": "arn:aws:apigateway:ap-south-1:lambda:path/2015-03-31/functions/arn:aws:lambda:ap-south-1:539977196287:function:driver_delete_fun/invocations",
      "responses": {
        "default": {
          "statusCode": "200"
        }
      },
      "passthroughBehavio r": "when_no_match",
      "httpMethod": "POST",
      "contentHandling": "CONVERT_TO_TEXT",
      "type": "aws"
    }
  }
 },

}, "securityDefinitions": {

"CognitoAuth": {
  "type": "apiKey",
  "name": "Authorization",
  "in": "header",
  "x-amazon-apigateway-authtype": "cognito_user_pools",
  "x-amazon-apigateway-authorizer": {
    "providerARNs": [
      "arn:aws:cognito-idp:ap-south-1:539977196287:userpool/ap-south-1_6j7axGXVm"
    ],
    "type": "cognito_user_pools"
  }
},
"lambdaAuth":{
  "type": "apiKey",
  "name": "Authorization",
  "in": "header",
  "x-amazon-apigateway-authtype": "custom",
  "x-amazon-apigateway-authorizer": { 
     "authorizerUri":  "arn:aws:apigateway:ap-south-1:lambda:path/2015-03-31/functions/arn:aws:lambda:ap-south-1:539977196287:function:my-service-dev-hello/invocations",
     "authorizerResultTtlInSeconds":1,
      "identitySource": "method.request.header.Authorization",
      "type": "request"
   }
}

}, "definitions": {

"Empty": {

  "type": "object",

  "title": "Empty Schema"
}

} }

1

1 Answers

1
votes

Cloudformation does not compare the contents of your s3 object when you specify an S3 uri in DefinitionUri. You can instead specify a relative local path and have cloudformation cli upload it for you just as it usually does with function code. Cloudformation will then use your file hash in the s3 object key, hence having different DefinitionUri and CodeUri everytime they contain changes.

Therefore, instead of having these:

DefinitionUri: s3://devdeliforcemumbailambda/swagger-json-testapi.json
CodeUri: build/authorizer.zip

do this:

DefinitionUri: ./swagger-json-testapi.json
CodeUri: ./authorizer-code-directory

What I wrote is applicable to the following: (up to date docs here)

BodyS3Location property for the AWS::ApiGateway::RestApi resource

Code property for the AWS::Lambda::Function resource

CodeUri property for the AWS::Serverless::Function resource

DefinitionUri property for the AWS::Serverless::Api resource

SourceBundle property for the AWS::ElasticBeanstalk::ApplicationVersion resource

TemplateURL property for the AWS::CloudFormation::Stack resource

Deploy your stack as follows:

aws cloudformation package --template-file template.yaml --s3-bucket devdeliforcemumbailambda --output-template-file packaged-template.yaml

aws cloudformation deploy --capabilities CAPABILITY_NAMED_IAM --stack-name test-swagger --template-file packaged-template.yaml