serverless-application-model icon indicating copy to clipboard operation
serverless-application-model copied to clipboard

DependsOn AWS::Serverless:Api does not wait for all resources

Open neonsamurai opened this issue 8 years ago • 31 comments

Background

I try deploying a serverless application which has a couple of Lambdas, an API gateway and I want to lock it behind an API key.

When sending the definition to cloudformation with aws cloudformation deploy I notice that DependsOn does wait on the RestApi resource to be ready, but it does not wait on the AWS:ApiGateway::Deployment resource which also is created by SAM (but transparently).

My SAM.yaml for reference:

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: Some API key protected serverless application
Parameters:
  targetStage:
    Description: Define stage to which Lambdas/API Gateways should be deployed.
    Type: String
    Default: dev
    AllowedValues:
      - dev
      - test
      - prod
    ConstraintDescription: Only stages dev, test, prod are allowed
Globals:
  Function:
    Runtime: nodejs6.10
    MemorySize: 128
    Timeout: 6
Resources:
  createFunction:
    Type: 'AWS::Serverless::Function'
    Properties:
      Handler: create.handler
      CodeUri: ../dist/deployment_package.zip
      FunctionName: !Sub "${AWS::StackName}-createCertificate-${targetStage}"
      Runtime: nodejs6.10
      Description: Create or overwrite an IoT certificate with given params.
      Role: 'arn:aws:iam::1234:role/my-role'
  deleteFunction:
    Type: 'AWS::Serverless::Function'
    Properties:
      Handler: delete.handler
      CodeUri: ../dist/deployment_package.zip
      FunctionName: !Sub "${AWS::StackName}-deleteCertificate-${targetStage}"
      Runtime: nodejs6.10
      Description: Delete an IoT certificate with given params.
      Role: 'arn:aws:iam::1234:role/my-role'
  restApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: !Ref targetStage
      DefinitionBody:
        swagger: "2.0"
        info:
          version: "2017-11-09T13:59:26Z"
          title: !Sub "${AWS::StackName}-api-${targetStage}"
        basePath: !Sub "/${targetStage}"
        schemes:
          - "https"
        paths:
          /certificate:
            post:
              responses: {}
              security:
              - api_key: []
              x-amazon-apigateway-integration:
                uri: !Sub "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${createFunction.Arn}/invocations"
                passthroughBehavior: "when_no_match"
                httpMethod: "POST"
                type: "aws_proxy"
            delete:
              responses: {}
              security:
              - api_key: []
              x-amazon-apigateway-integration:
                uri: !Sub "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${deleteFunction.Arn}/invocations"
                passthroughBehavior: "when_no_match"
                httpMethod: "POST"
                type: "aws_proxy"
        securityDefinitions:
          api_key:
            type: "apiKey"
            name: "x-api-key"
            in: "header"
        x-amazon-apigateway-binary-media-types:
          - "application/octet-stream"
  apiKey:
    Type: "AWS::ApiGateway::ApiKey"
    DependsOn:
      - restApi
    Properties:
      Name: !Sub "${AWS::StackName}-apiKey-${targetStage}"
      Description: !Sub "API key for: ${AWS::StackName} / ${targetStage}"
      Enabled: "true"
      StageKeys:
        - RestApiId: !Ref restApi
          StageName: !Ref targetStage

Expected behaviour

DependsOn used to wait on a AWS::Serverless:Api should wait on all resources created by SAM for the API.

Actual behaviour

This causes CloudFormation to throw a CREATE_FAILED | AWS::ApiGateway::ApiKey | apiKey | Invalid stage identifier specified error during deployment, since the stage which I want to associate with the API key does not exist yet and I have no means of referencing the (future) stage or referencing the AWS::ApiGateway::Deployment in the DependsOn.

Thoughts

The only workaround I can think of for now is to build all AWS::Serverless::Api component resources (RestApi, Deployment, ApiKey) by hand and use DependsOn on the respective resource identifiers.

neonsamurai avatar Feb 22 '18 08:02 neonsamurai

I have seen similar behavior. Thanks for the detailed write-up!

michaelj-smith avatar Feb 22 '18 18:02 michaelj-smith

So I spend basically the whole day figuring out how to deploy my stack. I found an okay workaround by inspecting the stack log in the CloudFormation console. I noticed that the stage which is associated with the deployment is reliably created last and that this stage always has the name ${AWS::StackName}Stage so that I can reliably use DependsOn with that stage. This of course will only work as long as the naming convention within SAM won't change. So I hope we can get a fix soon.

neonsamurai avatar Feb 22 '18 18:02 neonsamurai

@neonsamurai Naming convention will never change. If we change it, all resources will be recreated which is something we never want to happen.

We also documented them here

jfuss avatar Feb 22 '18 18:02 jfuss

So just caught up on the complete issue here.

We should support some way to create ApiKeys. I thought this was part of #248 but doesn't look like it made it there. Maybe it should be?

Maybe another way to help in solving this would be to do something similar to what we did to surface Versions and Aliases for AWS::Serverless::Function (Ref: FucntionLocicalId.Version). I think that would help in being able to make DependsOn for resources we generate but not sure how this really interacts and works outside the the AWS::Serverless::* Types (it's doable but think there is some additional work to make this supported).

The suggestion you made about relying on the generated LogicalIds is the way to go (at least for now). I know of others that have suggested the same thing on other issues here. We have fully documented the generated resources we create here, so it is safe to depend on these naming conventions. Make sure to follow the docs, the stage logicalId is <AWS::Serverless::Api LogicalId>Stage not based on the AWS::StackName. So in your example it would be restApi<whatever Ref: targetStage resolves to>Stage.

Note: AWS::Serverless::API (explicit) and AWS::Serverless::Function with an API Event type have different generates of resources. Please consult the documentation for references.

jfuss avatar Feb 22 '18 19:02 jfuss

This is helpful. Thank you for the docs! That provides a way to work around this. For the long term, I believe it would be best to auto-generate additional DependsOn properties for the auto-generated resources. So, the sam package step would modify any other resources that depend on the SAM resources, and we'd get package output like:

  apiKey:
    Type: "AWS::ApiGateway::ApiKey"
    DependsOn:
      - restApi
      - restApidevStage
      - restApiDeploymentSHA

I recognize this may be difficult, so I submit it an idea only.

michaelj-smith avatar Feb 22 '18 22:02 michaelj-smith

A little thing I noticed in the docs referenced by @jfuss :

It says the logical ID of the stage resource would be: <AWS::Serverless::Api.LogicalId><AWS::Serverless::Api.StageName>Stage whereas it appears from the CloudFormation console it is just <AWS::Serverless::Api.LogicalId>Stage as @jfuss mentioned in his post

neonsamurai avatar Feb 23 '18 10:02 neonsamurai

So, both of these are true. If StageName is a straight up string, then it will be <ID><StageName>Stage. If it is an intrinsic function, it will be <ID>Stage. This is because SAM cannot reliably resolve intrinsic functions. SAM runs outside of CloudFormation where the actual intrinsic function resolution happens.

sanathkr avatar Feb 23 '18 17:02 sanathkr

@neonsamurai: I am also stuck with same problem Can u please guide me

I can reliably use DependsOn with that stage. This of course will only work as long as the naming convention within SAM won't change. So I hope we can get a fix soon. Or share On what actually u depends on with the snippet

manishverma18 avatar Aug 05 '18 18:08 manishverma18

Is this issue going to be addressed? It's been open for almost a year now and it's a roadblock in using SAM template when you want to secure your API endpoints with Cognito.

caliniliescu avatar Oct 19 '18 14:10 caliniliescu

Is this issue going to be addressed? It's been open for almost a year now and it's a roadblock in using SAM template when you want to secure your API endpoints with Cognito.

I found that the problem was being caused by bad indentation of swagger definition written inline in my yml sam template. The error i was getting from cloudformation made me think it was a dependency issue.

caliniliescu avatar Oct 22 '18 12:10 caliniliescu

Does the provided workaround not work for you?

brettstack avatar Oct 22 '18 14:10 brettstack

@neonsamurai Naming convention will never change. If we change it, all resources will be recreated which is something we never want to happen.

We also documented them here

This link doesn't work. Can you please provide an updated link?

judahb avatar Dec 17 '18 22:12 judahb

Updated link: https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst

keetonian avatar Dec 19 '18 18:12 keetonian

So I spend basically the whole day figuring out how to deploy my stack. I found an okay workaround by inspecting the stack log in the CloudFormation console. I noticed that the stage which is associated with the deployment is reliably created last and that this stage always has the name ${AWS::StackName}Stage so that I can reliably use DependsOn with that stage. This of course will only work as long as the naming convention within SAM won't change. So I hope we can get a fix soon.

I've created a workaround solution based on @neonsamurai comments and seems it's working properly. So, I have on my template.yaml something similar to:

MyFunction:
  Type: AWS::Serverless::Function
  Properties:
    Events:
      MyFunctionApi:
        Type: Api
        Properties:
          Path: "/functions"
          Method: POST
          RestApiId: !Ref MyApi  

MyApi:
  Type: "AWS::Serverless::Api"
  Properties:
    StageName: dev 
    Cors: "'*'"  

I was expecting the full API resources created so I would be able to create a "base path mapping" as below:

MyApiBasePathMapping:
    Type: "AWS::ApiGateway::BasePathMapping"
    DependsOn:
      - MyFunctionMyFunctionApiPermissionStage
    Properties:
      BasePath: "services"
      DomainName: "www.example.com"
      RestApiId: !Ref MyApi
      Stage: dev

The AWS::ApiGateway::BasePathMapping expects the API Stages created to work properly. So I realise the stage is created with the following structure after checking on the stack events:

{Function.LogicalId}{Function.Events.LogicalId}PermissionStage

Hopefully, there's a better and more reliable way to implement it soon.

andrepossebomgarcia avatar Aug 28 '19 07:08 andrepossebomgarcia

@jfuss for AWS::Serverless::Api resource now supports getting Stage and Deployment using following:

  • !Ref: MyApi.Stage
  • !Ref: MyApi.Deployment

However, issue still persists as DependsOn attribute doesn't support usage of an intrinsic function(Ref in our case) for any of its value(s).

Possible approaches towards resolution might include:

  1. Let CloudFormation mark Serverless::API complete only if all the associated resources (RestApi,Stage,Deployment) have finished their creation. Not a good approach(even if possible) as AWS::ApiGateway::RestApi resource has it own Lifecycle and CloudFormation actually has nothing to do with Serverless type resources lifecycle but translated/transformed CloudFormation specific resources.
  2. Adding support/exception in DependsOn attribute for Serverless type resources so that !Ref can be used to get Stage/Deployment logical Ids.

razhamma avatar Aug 31 '19 11:08 razhamma

I have encountered this issue too. It's very annoying. It's unfortunate that after a year still the problem. Solution provided by @sanathkr and @jfuss sometimes works and sometimes doesn't. My workaround was to depends my resource on a function (which is itself dependent on api gatewqay). Here is an example:

  MyACLassociation:
    Type: AWS::WAFRegional::WebACLAssociation
    DependsOn: ALambdaFunctionWhichIsTriggeredByApiGateway
    Properties: 
      ResourceArn: AnaRN
      WebACLId: !Ref MyWebACL

azarboon avatar Sep 03 '19 06:09 azarboon

@azarboon you can use a reference to the api and the Stage which will add an implicit dependsOn. Here is an example of how to use an api key:

  MyFirstApiKey:
    Type: AWS::ApiGateway::ApiKey
    DependsOn:
      - MyUsagePlan
    Properties:
      Enabled: true
      StageKeys:
        - RestApiId:
            Ref: MyApi
          StageName:
            Ref: MyApi.Stage

Here is another example for using BasePathMapping:

MyApiBasePathMapping:
    Type: "AWS::ApiGateway::BasePathMapping"
    Properties:
      BasePath: "services"
      DomainName: "www.example.com"
      RestApiId: !Ref MyApi
      Stage: !Ref MyApi.Stage

Please let us know if this works for you

ShreyaGangishetty avatar Sep 06 '19 17:09 ShreyaGangishetty

@azarboon you can use a reference to the api and the Stage which will add an implicit dependsOn. Here is an example of how to use an api key:

  MyFirstApiKey:
    Type: AWS::ApiGateway::ApiKey
    DependsOn:
      - MyUsagePlan
    Properties:
      Enabled: true
      StageKeys:
        - RestApiId:
            Ref: MyApi
          StageName:
            Ref: MyApi.Stage

Here is another example for using BasePathMapping:

MyApiBasePathMapping:
    Type: "AWS::ApiGateway::BasePathMapping"
    Properties:
      BasePath: "services"
      DomainName: "www.example.com"
      RestApiId: !Ref MyApi
      Stage: !Ref MyApi.Stage

Please let us know if this works for you

Hi,

Thanks for reply but I'm afraid it doesn't work for my usecase. I cannot use intrinsic function (MyApi.Stage) in DependsOn, as far as I know

azarboon avatar Sep 09 '19 12:09 azarboon

I have encountered this issue too. It's very annoying. It's unfortunate that after a year still the problem. Solution provided by @sanathkr and @jfuss sometimes works and sometimes doesn't. My workaround was to depends my resource on a function (which is itself dependent on api gatewqay). Here is an example:

MyACLassociation: Type: AWS::WAFRegional::WebACLAssociation DependsOn: ALambdaFunctionWhichIsTriggeredByApiGateway Properties: ResourceArn: AnaRN WebACLId: !Ref MyWebACL

Update: seems my workaround fails also. Other workaround is this: deploy the stack without the dependent resource (i.e. AWS::WAFRegional::WebACLAssociation), then add the dependent resource and redeploy the stack. Obviously this is an annoying workaround. Looking forward the SAM team to fix this issue ASAP.

azarboon avatar Sep 09 '19 12:09 azarboon

Another sad workaround that I've seen function is to take in a parameter like IsNotFirstRun, setup a Conditions on it, and then set a Condition on the resource on that conditional.

tomislacker avatar Sep 09 '19 12:09 tomislacker