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

feat: add AlwaysDeploy to AWS::Serverless::Api #2935

Merged
merged 27 commits into from
Feb 28, 2023
Merged

feat: add AlwaysDeploy to AWS::Serverless::Api #2935

merged 27 commits into from
Feb 28, 2023

Conversation

hoffa
Copy link
Contributor

@hoffa hoffa commented Feb 23, 2023

Issue #, if available

#660

And the ones deduplicated into the above:

  1. Renaming a Serverless::Function used by an Api Gateway does not deploy the Gateway #634
  2. Issue with API Gateway returning 500 server error after renaming function. #930
  3. API Gateway Stages Deployment only gets updated when Swagger is updated #479
  4. AWS::Serverless::Api deployment resource is not generated on Sub'ed Ref change #914
  5. Updating API resource policy using mappings #1162
  6. APIs are not getting deployed on Prod stage #1561
  7. Changing minimumCompressionSize doesn't trigger API deployment. #1447
  8. Bug: APIG Stage deployment isn't triggered on Lambda code update #2888

Description of changes

Add new property AlwaysDeploy (disabled by default) to AWS::Serverless::Api that always creates new deployment.

Using the time instead of something deterministic (e.g. hash of template and parameters) in case something outside of SAM's visibility changes (e.g. S3 object).

Description of how you validated changes

Based on template from #2888, save as template.yaml:

Transform: AWS::Serverless-2016-10-31
Resources:
  MyFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: index.handler
      InlineCode: |
        exports.handler = async function (event) {
          return {
            statusCode: 200,
            body: JSON.stringify({ message: "Hello, foo!" }),
          }
        }
      Runtime: nodejs16.x
      AutoPublishAlias: live

  MyRole: 
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument: 
        Version: "2012-10-17"
        Statement: 
        - Effect: Allow
          Principal: {Service: "apigateway.amazonaws.com"}
          Action: sts:AssumeRole
      Policies: 
      - PolicyName: AllowInvokeLambdaFunctions
        PolicyDocument: 
          Version: "2012-10-17"
          Statement: 
          - Effect: Allow
            Action: lambda:InvokeFunction
            Resource: "*"

  MyApi:
    Type: AWS::Serverless::Api
    Properties:
       StageName: dev
       DefinitionBody:
        openapi: 3.0.0
        paths:
          /hello:
            get:
              x-amazon-apigateway-integration:
                credentials: !GetAtt MyRole.Arn
                uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${MyFunction.Version}/invocations
                passthroughBehavior: when_no_match
                httpMethod: POST
                type: aws_proxy

Outputs:
  WebEndpoint:
    Description: "API Gateway endpoint URL"
    Value: !Sub "https://${MyApi}.execute-api.${AWS::Region}.amazonaws.com/dev/hello"

Deploy:

../serverless-application-model/bin/sam-translate.py --template-file template.yaml && sam deploy --region us-west-2 --resolve-s3 --capabilities CAPABILITY_IAM --stack-name test-always-deploy --template transformed-template.yaml

curl endpoint:

{"message":"Hello, foo!"}

Now change the Lambda:

sed -i.bak 's/Hello, foo!/Hello, bar!/g' template.yaml

Redeploy:

../serverless-application-model/bin/sam-translate.py --template-file template.yaml && sam deploy --region us-west-2 --resolve-s3 --capabilities CAPABILITY_IAM --stack-name test-always-deploy --template transformed-template.yaml

But the endpoint still shows the old output:

{"message":"Hello, foo!"}

Now, testing the fix, change template.yaml as such:

--- before.yaml	2023-02-28 10:19:46
+++ template.yaml	2023-02-28 10:20:03
@@ -35,6 +35,7 @@
   MyApi:
     Type: AWS::Serverless::Api
     Properties:
+       AlwaysDeploy: true
        StageName: dev
        DefinitionBody:
         openapi: 3.0.0

And redeploy:

../serverless-application-model/bin/sam-translate.py --template-file template.yaml && sam deploy --region us-west-2 --resolve-s3 --capabilities CAPABILITY_IAM --stack-name test-always-deploy --template transformed-template.yaml

And curl the endpoint again (sometimes takes a few seconds to change), and we get the proper output:

{"message":"Hello, bar!"}

Checklist

Examples?

Please reach out in the comments if you want to add an example. Examples will be
added to sam init through aws/aws-sam-cli-app-templates.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@hoffa hoffa changed the title feat: AlwaysDeploy feat: AlwaysDeploy API Feb 24, 2023
@hoffa hoffa changed the title feat: AlwaysDeploy API feat: add AlwaysDeploy to AWS::Serverless::Api Feb 24, 2023
@hoffa hoffa marked this pull request as ready for review February 25, 2023 00:46
@hoffa hoffa requested a review from a team as a code owner February 25, 2023 00:46
@hoffa hoffa marked this pull request as draft February 25, 2023 00:46
@hoffa hoffa marked this pull request as ready for review February 28, 2023 18:31
samtranslator/model/apigateway.py Outdated Show resolved Hide resolved
samtranslator/model/sam_resources.py Show resolved Hide resolved
tests/translator/test_translator.py Show resolved Hide resolved
@xazhao
Copy link
Contributor

xazhao commented Feb 28, 2023

How about AWS::Serverless::HttpApi?

@hoffa
Copy link
Contributor Author

hoffa commented Feb 28, 2023

How about AWS::Serverless::HttpApi?

I haven't seen a similar issue reported for it. AWS::ApiGatewayV2::Stage has AutoDeploy FWIW.

@hoffa hoffa merged commit c3c04dd into aws:develop Feb 28, 2023
@hoffa hoffa deleted the always-deploy branch February 28, 2023 19:45
GavinZZ pushed a commit to GavinZZ/serverless-application-model that referenced this pull request Mar 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants