I have a very simple YAML file to build an API Gateway and a lambda function. If I use straight cloud formation commands, it works fine.
If I try to run it with sam, the build takes forever - 10 minutes or so - but does finally conclude. However, the deploy does not work.
Here is the YAML file
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: Storygraf backend API
Globals:
Function:
Timeout: 3
Resources:
ExpressApi:
Type: AWS::Serverless::Api
Properties:
StageName: dev
ExpressLambdaFunction:
Type: AWS::Serverless::Function
Properties:
Environment:
Variables:
CodeUri: ./
Handler: lambda.handler
MemorySize: 512
Runtime: nodejs14.x
Timeout: 30
Events:
ProxyApiRoot:
Type: Api
Properties:
RestApiId: !Ref ExpressApi
Path: /
Method: ANY
cors: true
ProxyApiGreedy:
Type: Api
Properties:
RestApiId: !Ref ExpressApi
Path: /{proxy+}
Method: ANY
cors: true
SAM Commands
sam build
sam deploy --guided
Error
Error: Invalid value for '--parameter-overrides': is not in valid format. It must look something like 'ParameterKey=KeyPairName,ParameterValue=MyKey ParameterKey=InstanceType,ParameterValue=t1.micro' or 'KeyPairName=MyKey InstanceType=t1.micro'
I have no idea what this error means.
Related
I am using AWS SAM to test my Api gateway and lambdas locally.
When executing sam local start-api and calling a lambda, I'd like the event to be of version 2.0 format instead of version 1.
I am using CDK HttpApi construct from #aws-cdk/aws-apigatewayv2 hence there is now inconsistency between my local testing and what's deployed.
I am new to Sam config, my template.yml file is:
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Resources:
MyFunction:
Type: 'AWS::Serverless::Function'
Properties:
Handler: index.handler
Runtime: nodejs14.x
CodeUri: .output/healthz
Timeout: 10
Events:
ApiEvent:
Type: Api
Properties:
Path: /health
Method: GET
Globals:
HttpApi:
CorsConfiguration:
AllowOrigin: "'http://localhost:3000'"
AllowMethods: "'POST, OPTIONS, GET, PUT'"
AllowHeaders: "'Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token'"
I've tried various setups (Api, HttpApi) using those AWS Docs for SAM but always manage to get only version 1 event.
Can you point me to what I am doing wrong or how to specify the version?
I found the resolution in this post.
It is necessary to specify PayloadFormatVersion: "2.0" (value as string) in the configuration of HttpApi.
Example yml:
Resources:
MyFunction:
Type: 'AWS::Serverless::Function'
Properties:
Handler: index.handler
Runtime: nodejs14.x
CodeUri: .output/healthz
Timeout: 10
Events:
ApiEvent:
Type: HttpApi
Properties:
PayloadFormatVersion: "2.0"
Path: /health
Method: GET
Auth:
Authorizer: NONE
I am trying to have a customer name given to my API gateway created through SAM template.
and also restrict creating only one stage while deploying.
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: An AWS Serverless Specification template describing your function.
Resources:
ApiGatewayApi:
Type: AWS::Serverless::Api
Properties:
StageName: Development
Cors: "'*'"
GetAllUser:
Type: AWS::Serverless::Function
Properties:
FunctionName: loadeo_get_all_user
CodeUri: code/
Handler: get_all_user.lambda_handler
Timeout: 5
Runtime: python3.8
Role: lambda_execution
MemorySize: 128
Events:
GetAllUser:
Type: Api
Properties:
Path: /get-all-user
Method: get
RestApiId:
Ref: ApiGatewayApi
All is working fine as I want, but
It is creating the API with the name of the stack (I want to give a custom name)
Along with the "Development" it is also adding "stage" while deploying.
How I can achieve these two cases?
To specify Name for your ApiGatewayApi, you have to use Name property:
A name for the API Gateway RestApi resource
Thus your ApiGatewayApi would be:
ApiGatewayApi:
Type: AWS::Serverless::Api
Properties:
StageName: Development
Name: MyApiName
Cors: "'*'"
I'm not sure I understand the second issue, thus can't comment on it right now.
As explained here you can add the following to fix the Stage issue:
Globals:
Api:
OpenApiVersion: 3.0.1
I am trying my hands on SAM templates.
Here I am trying to figure out how can I add custom names within the template.
As when I package and Deploy the Template it creates the lambda function with an added alphanumeric value.
API gateway name will be the stack name and it will be deployed in "Prod" and "Stage" Stage with in the API gateway.
Is there a way to have my own custom names.
Here is my sample code for the SAM template:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: An AWS Serverless Specification template describing your function.
Resources:
GetAllUser:
Type: AWS::Serverless::Function
Properties:
CodeUri: code/
Handler: getAllUser.lambda_handler
Timeout: 5
Runtime: python3.8
Role: lambda_execution
MemorySize: 128
Events:
GetAllUser:
Type: Api
Properties:
Path: /get-all-user
Method: get
could any one help me with this?
For creating a name for your lambda you can specify directly AWS::Serverless::Function
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: An AWS Serverless Specification template describing your function.
Resources:
GetAllUser:
Type: AWS::Serverless::Function
Properties:
FunctionName: "MyFunctionName"
CodeUri: code/
Handler: getAllUser.lambda_handler
Timeout: 5
Runtime: python3.8
Role: lambda_execution
MemorySize: 128
Events:
GetAllUser:
Type: Api
Properties:
Path: /get-all-user
Method: get
As for other names like StageName and ApiGateway Name, you need to use
AWS::Serverless::Api
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: AWS SAM template with a simple API definition
Resources:
ApiGatewayApi:
Type: AWS::Serverless::Api
Properties:
StageName: prod
Name: myapi
ApiFunction: # Adds a GET api endpoint at "/" to the ApiGatewayApi via an Api event
Type: AWS::Serverless::Function
Properties:
FunctionName: myfunction
Events:
ApiEvent:
Type: Api
Properties:
Path: /
Method: get
RestApiId:
Ref: ApiGatewayApi
Runtime: python3.7
Handler: index.handler
InlineCode: |
def handler(event, context):
return {'body': 'Hello World!', 'statusCode': 200}
I get the following error upon attempting to sam deploy --guided my lambda application.
Error: Failed to create changeset for the stack: {stack-name}, An error occurred (ValidationError) when calling the CreateChangeSet operation: Stack:arn:aws:cloudformation:ap-southeast-2:014009325916:stack/{stack-name}/f2212bf0-bb41-11ea-8ef3-0aa7af0536b6 is in ROLLBACK_COMPLETE state and can not be updated.
Some further context into this issue is that all of my lambda functions do not have authorization defined. Not sure if that is related but I'm stating it in the case that it is.
I frankly have no idea how to go about this issue. Any help is appreciated.
===
Context: Building an automated trading system
Request for Cloudformation Template:
yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: A tradingview alert wrapper that interprets alerts and makes trades according to them.
Globals:
Function:
# CodeUri: function/.
# Runtime: python3.8
# Policies:
# - AWSLambdaFullAccess
# Tracing: Active
# Timeout: 30
Environment:
Variables:
APIKEY: ""
SECRETKEY: ""
Resources:
tradeFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.trade
Description: The primary execution function
CodeUri: function/.
Runtime: python3.8
Policies:
- AWSLambdaFullAccess
Tracing: Active
Timeout: 60
Events:
inputResponse:
Type: Api
Auth:
ApiKeyRequired: false
Authorizer: NONE
Properties:
Path: /trade
Method: post
printAccountBalanceFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: function/.
Runtime: python3.8
Policies:
- AWSLambdaFullAccess
Tracing: Active
Timeout: 30
Handler: app.print_account_balance
Description: Returns account balance over time or trade
Events:
inputResponse:
Type: Api
Properties:
Path: /print_account_balance
Method: get
startTradesFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: function/.
Runtime: python3.8
Policies:
- AWSLambdaFullAccess
Tracing: Active
Timeout: 30
Handler: app.start_trades
Description: Resets trade log for a fresh start
Events:
inputResponse:
Type: Api
Properties:
Path: /start_trades
Method: get
retrieveTradeLogFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: function/.
Runtime: python3.8
Policies:
- AWSLambdaFullAccess
Tracing: Active
Timeout: 30
Handler: app.retrieve_trade_log
Description: Return all the trade logs in the dyanamoDB database
Events:
inputResponse:
Type: Api
Properties:
Path: /retrieve_trade_log
Method: get
The error is encountered to my understanding when one does an initial deployment and that initial deployment fails. As a result of this, something breaks and the deployment service can not execute successfully. To fix this issue, simple delete the aws cloudformation stack and redeploy.
One can use the following command to delete:
aws cloudformation delete-stack --stack-name <insert stack-name>
Reference
Might need to run sam build first so your resources can get the latest changes
I'm trying to set up a local dev environment for my Lambda functions using SAM. I had everything working until I added a reference to a layer in my configuration.
I followed the instructions here: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-layers.html. I added my ARN for my layer version in my template.ymal as follows:
# template.ymal
TestLayerFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: TestLayer
Role: arn:aws:iam::111111111111:role/ReadStreamingTable
CodeUri: src/streaming/test-layer/
Handler: app.handler
Runtime: nodejs8.10
Layers:
- arn:aws:lambda:eu-west-1:111111111111:layer:Global:7
However when running a "sam local invoke" I get the following error:
botocore.exceptions.EndpointConnectionError: Could not connect to the endpoint URL:
"https://lambda.eu-west-1a.amazonaws.com/2018-10-31/layers/arn%3Aaws%3Alambda%3Aeu-west-1%3A111111111111%3Alayer%3AGlobal/versions/7"
The way I've added the layer ARN in the configuration seems to be exactly how they do it in the example so I'm not sure what is causing the error.
I know it's not exactly a solution but can you not have your layer as part of your SAM file?
If you have a look on this article on the AWS site they use both the layer and the lambda function on the same yaml file so you'd end up with something like this:
Resources:
TempConversionFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: hello-world/
Handler: app.lambdaHandler
Layers:
- !Ref TempConversionDepLayer
Events:
HelloWorld:
Type: Api
Properties:
Path: /{conversion}/{value}
Method: get
TempConversionDepLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: sam-app-dependencies
Description: Dependencies for sam app [temp-units-conv]
ContentUri: dependencies/
CompatibleRuntimes:
- nodejs6.10
- nodejs8.10
LicenseInfo: 'MIT'
RetentionPolicy: Retain