-
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
Swagger templates don't get transformed when using DefinitionUri #305
Comments
I'm running into the same issue as well! |
Same issue here, Trying to figure out the cleanest way to solve this problem. |
@AlexThomas90210 Until cloudformation package supports it, I added an |
I've encountered this issue as well. Eagerly waiting for sam to support this. I can't see any reasonable use for |
@oharaandrew314 I kinda agree; I was able to get things working normally without the swagger way; but instead implicitly defining the api endpoints within the sam template.yml but I just thought it was cleaner with swagger.
My expectation would be that the swagger definition file would take care of creating the API as well as pointing to the right lambdas through the Fn::Sub: right above. Could anyone tell me why one would choose the Swagger way over the other way which is to just list out all functions and endpoints in the sam template and let sam create the api for you implicitly? |
@dvdmmc would you be able to share your sam template and swagger file for reference? I'm using CodeStar and I'm pretty confident my setup should work; it's just failing with some INTERNAL ERROR which are not specific enough for me to find out what the issue is! |
I saw in a few other threads that CORS support is high priority to SAM, so what I ended up doing is just inlining the definition body in the sam template and then just referencing the API in the Events part of a sam function. It makes the sam template much larger than it should be, but hopefully, sam will support "cors: true" soon the same way the serverless.com framework does. Then I can remove it. Not really a solution for people who do want to define their own swagger files though. |
This is a current blocker for us too. There is no way to use aws cloudformation package and DefinitionBody as it's not a field that has it's reference substituted. https://docs.aws.amazon.com/cli/latest/reference/cloudformation/package.html |
Sorry @Joel-fogue this fell off my radar a bit. In case you or anyone else is still interested, here is how I've set up my CloudFormation template:
In your Swagger template, you can then do things like:
the important step is to have the swagger template copied to an S3 bucket, so my build step for CodeBuild looks something like:
Hope this helps, sorry for the late response. |
Having the exact same problem |
Any news on this issue ? We just came across the same problem. For me the solution is for the aws cloudformation package command to upload your swagger file the same way it does with the function CodeUri. |
Hi all. Its been seven months. Any updates on this? |
I've reached out to the API Gateway team to see if they will fix the underlying issue. However, if they are unable to fix it we may be able to do something like this:
DefinitionUri:
Path: src/swagger.yml # Also accepts S3
Inline: true # Default: true
Upload: false # Default: false
We'll create an RFC for this and accept contributions if people think this will solve their problem. |
@brettstack Thank you. This will be helpful. |
It will work if you just stick the Swagger inline in DefinitionBody or use the workaround posted above by dvdmmc |
@brettstack
And the response from
The offending lines are:
and this
If either block is present, I get the invalid swagger error. If both are commented out, I get:
Perhaps I am doing something wrong. |
SAM CLI has a bug in the validate command when you don't specify a DefinitionBody or DefinitionUri aws/aws-sam-cli#803. It should deploy fine though? |
Discussed internally. Here are some notes:
|
Will this also get rid of the following error we get while using Auth: "Auth works only with inline Swagger specified in 'DefinitionBody' property" Currently, this is not working with the workaround you proposed. |
@brettstack I tried your approach of Inline:true but sam package didn't replace DefinitionUri with DefinitionBody and it didn't replace it with inline swagger. I am using sam version 0.15.0. |
@akhilkvpv88 The |
I did some investigating on having SAM add a Investigation Details: I created and tested the new First TestAWSTemplateFormatVersion: '2010-09-09'
Transform:
- 'AWS::Serverless-2016-10-31'
Parameters:
StageName:
Default: 'prod'
Type: String
Resources:
LambdaAPIDefinition:
Type: AWS::Serverless::Api
Properties:
StageName: !Ref StageName
DefinitionInclude: s3://bucket/swagger.json This test transforms into the following JSON API definition: "LambdaAPIDefinition": {
"Type": "AWS::ApiGateway::RestApi",
"Properties": {
"Body": {
"Fn::Transform": {
"Name": "AWS::Include",
"Parameters": {
"Location": "s3://bucket/swagger.json"
}
}
}
}
}
}, This resulted in the following error:
It looks like CFN wasn't expecting another transform after the SAM transform, so... Second TestMy second test included having SAM do the same as the previous step as well as change
Third TestI expanded the Transform:
- 'AWS::Serverless-2016-10-31'
- 'AWS::Include' This should have forced CFN to run the Include transform after SAM (docs), but it didn't, instead failing with
Next Steps |
Any update on the proposed new feature is there a PR yet? |
We haven't found a simple way to make this work as part of the SAM transform, we are still following up with CloudFormation team. If that doesn't work out, the next best option would be to change the sam cli package command to inline swagger that is referenced in a separate file. |
@praneetap any updates on this? |
Related: aws/aws-cli#3454 |
I'd recommend using the Here's an example: MyApi:
Type: AWS::Serverless::Api
Properties:
DefinitionBody:
Fn::Transform:
Name: AWS::Include
Parameters:
Location: openapi.yaml |
I did a CodeBuild a few days ago with a template.yml
buildspec.yml
|
I can confirm along with @joquijada this does work with our build pipeline too (via Jenkins, though it uses regular AWS CloudFormation/SAM commands). Interestingly the AWS::Include documentation says this:
All the examples show S3, and but it does indeed work with a local URI: template.yaml
It looks like the CLI tools are now smart enough to upload the file to the same S3 bucket and reference it. This was definitely NOT the case in late July 2019 because I had to write a workaround to upload the Swagger spec to S3, then pass the S3 URI to the stack as a parameter for use in the CloudFormation Service > Select Stack > Template Tab
|
I can confirm this as well. I have deployed a solution like that multiple times as well. |
I got everything working with this setup - took me a good few hours to figure everything out - so I hope this saves someone some time. This demos two endpoints each pointing to different functions. Dev:
Deploy:
Delete:
Template.yaml AWSTemplateFormatVersion : "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Globals:
Function:
Timeout: 5
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: node-world/
Handler: app.lambdaHandler
Runtime: nodejs12.x
HelloMoonFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: node-moon/
Handler: app.lambdaHandler
Runtime: nodejs12.x
API:
Type: AWS::Serverless::Api
Properties:
StageName: Test
DefinitionBody:
Fn::Transform:
Name: AWS::Include
Parameters:
Location: swagger-node.yaml
HelloWorldFunctionPermission:
Type: AWS::Lambda::Permission
DependsOn:
- API
- HelloWorldFunction
Properties:
Action: lambda:InvokeFunction
FunctionName: !Ref HelloWorldFunction
Principal: apigateway.amazonaws.com
HelloMoonFunctionPermission:
Type: AWS::Lambda::Permission
DependsOn:
- API
- HelloMoonFunction
Properties:
Action: lambda:InvokeFunction
FunctionName: !Ref HelloMoonFunction
Principal: apigateway.amazonaws.com swagger-node.yaml swagger: '2.0'
info:
version: 1.0.0
title: Test API
description: Test API
paths:
/node-world:
get:
description: Test Hello World
produces:
- application/json
responses:
"200":
description: Test API
schema:
"$ref": "#/definitions/Message"
x-amazon-apigateway-integration:
type: aws_proxy
httpMethod: POST
uri:
Fn::Sub: "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HelloWorldFunction.Arn}/invocations"
/node-moon:
get:
description: Test Hello Moon
produces:
- application/json
responses:
"200":
description: Test API
schema:
"$ref": "#/definitions/Message"
x-amazon-apigateway-integration:
type: aws_proxy
httpMethod: POST
uri:
Fn::Sub: "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HelloMoonFunction.Arn}/invocations"
definitions:
Message:
type: object
properties:
message:
type: string samconfig.toml version = 0.1
[default]
[default.deploy]
[default.deploy.parameters]
stack_name = "TfTestStack"
s3_bucket = "xxx"
s3_prefix = "TfTestStack"
region = "xxx"
profile = "xxx"
confirm_changeset = true
capabilities = "CAPABILITY_IAM" /node-<world|moon>/app.js // const axios = require('axios')
// const url = 'http://checkip.amazonaws.com/';
let response;
exports.lambdaHandler = async (event, context) => {
try {
// const ret = await axios(url);
response = {
'statusCode': 200,
'body': JSON.stringify({
message: 'hello xxx',
// location: ret.data.trim()
})
}
} catch (err) {
console.log(err);
return err;
}
return response
}; |
Thanks @dacgray for providing a working example for the issue. Closing as there is already a solution. Please use |
Hi,
I'm trying to package a swagger spec using the flow proposed by SAM, but it doesn't do the transform properly.
When specifying an
AWS::Serverless:Api
resource, I have two options for the swagger spec:DefinitionUri
DefinitionBody
When I use
DefinitionUri
, then any references to Lambda functions in my swagger document will not get transformed to the actual ARNs of the Lambda functions, e.g. (in swagger.yaml)According to the swagger CORS example in the SAM examples folder, you use
DefinitionBody
. The problem with usingDefinitionBody
is that theaws cloudformation package
command does not transform any references to a local swagger file into a remote file in S3.The workaround is that I actually put the swagger spec in S3 myself, but this is clunky and contrary to the existing model.
The text was updated successfully, but these errors were encountered: