-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
DependsOn AWS::Serverless:Api does not wait for all resources #313
Comments
I have seen similar behavior. Thanks for the detailed write-up! |
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 |
@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 |
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 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 < Note: |
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
I recognize this may be difficult, so I submit it an idea only. |
A little thing I noticed in the docs referenced by @jfuss : It says the logical ID of the stage resource would be: |
So, both of these are true. If StageName is a straight up string, then it will be |
@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. |
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. |
Does the provided workaround not work for you? |
This link doesn't work. Can you please provide an updated link? |
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:
I was expecting the full API resources created so I would be able to create a "base path mapping" as below:
The
Hopefully, there's a better and more reliable way to implement it soon. |
@jfuss for AWS::Serverless::Api resource now supports getting Stage and Deployment using following:
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:
|
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:
|
@azarboon you can use a reference to the api and the Stage which will add an implicit dependsOn. 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 |
Update: seems my workaround fails also. Other workaround is this: |
Another sad workaround that I've seen function is to take in a parameter like |
@azarboon If a resource has a reference to another resource, CFN understands that it depends on that other resource and you don't need to add the explicit "DependsOn" to it. |
That's how it should be and that's what I expected; but unfortunately AWS::Serverless::Api doesn't support this model. Despite it creates api stage and deployment; but when a resource of type AWS::Serverless::Api is in "done" status, it doesn't mean that "stage" is created. Yes, that really s..... This is what I experienced and what AWS Support investigated & confirmed too. |
@azarboon Just to make sure I understand, are you saying |
I tried following combination (third one was suggested by AWS support) but that didn't work either. As far as I know, for dependson, I must not use !Ref and just need to use logical name of resource (correct me if I'm wrong- I'm not next to my template and have memory glitch :D ):
|
@azarboon Look at this #313 (comment) |
Thanks. I'll try this and let you know if it works or not. |
This seemed to work for me Parameters:
StageName:
Default: 'dev'
Type: String
Resources:
MyLambdaFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: mylookupidentity
CodeUri: my_lambda.zip
Events:
APIGateway:
Type: Api
Properties:
Path: /myfn
Method: post
RestApiId: !Ref APIGateway
APIGatewayKey:
Type: AWS::ApiGateway::ApiKey
DependsOn:
- APIGatewayStage #LogicalId for API Gateway + 'Stage'
Properties:
Name: myAPI-api-key
Enabled: true
StageKeys:
- RestApiId: !Ref APIGateway
StageName: !Ref StageName
APIGateway:
Type: AWS::Serverless::Api
Properties:
Name: myAPI
StageName: !Ref StageName
MethodSettings:
- HttpMethod: "*"
LoggingLevel: INFO
ResourcePath: "/*"
DefinitionBody:
Fn::Transform:
Name: AWS::Include
Parameters:
Location: docs/api.yaml |
Hey @ShreyaGangishetty is this issue really fixed? Could you doublecheck? |
The bold lines fixed the issue for me
|
@umaragu how did you come up with Just spent a day trying to understand why my stack kept failing even though I had all the |
See https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-specification-generated-resources-api.html; it documents which resources are generated and how they're referenceable. |
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 thatDependsOn
does wait on theRestApi
resource to be ready, but it does not wait on theAWS:ApiGateway::Deployment
resource which also is created by SAM (but transparently).My SAM.yaml for reference:
Expected behaviour
DependsOn
used to wait on aAWS::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 theAWS::ApiGateway::Deployment
in theDependsOn
.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 useDependsOn
on the respective resource identifiers.The text was updated successfully, but these errors were encountered: