I am using AWS SAM to deploy a Lambda function. I'am using the AutoPublishAlias property to automatically publish a new version when I deploy the function but I'm getting the following error: Invalid function version 9. Function version 9 is already included in routing configuration. (Service: AWSLambdaInternal; Status Code: 400; Error Code: InvalidParameterValueException; Request ID: 56b59a6b-6c57-434e-a505-ce7aa27c99b6). Every time I delete and create the stack the function gets created successfully, but when I try to update the stack I get the error. I'm also wondering why is the version number not starting from 1 after deleting the Lambda?
The Lambda definition:
ApiLambda:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub '${TagApplication}-${TagEnvironment}-api-lambda'
CodeUri: ../build
Handler: lambda.handler
MemorySize: 256
Role: !GetAtt LambdaExecutionRole.Arn
Runtime: nodejs10.x
Timeout: 30
AutoPublishAlias: 'live'
ProvisionedConcurrencyConfig:
ProvisionedConcurrentExecutions: '1'
Screenshot of AWS Lambda console Alias menu:
Looks like you need to configure the DeploymentPreference property as well - see this link - https://github.com/awslabs/serverless-application-model/issues/1296 and https://github.com/jcts3/sam-pc-experiment/blob/master/template.yaml#L22
Related
I'm using AWS SAM to deploy a Lambda using cloudformation. The lambda uses the StartInstancesCommand,StopInstancesCommand and DescribeInstancesCommand api to turn instances on and off on schedule.
Using both the EC2DescribePolicy or EC2FullAccessPolicy SAM policy templates, I have been encountering the following error on:
sam deploy
1 validation error detected: Value
'EC2DescribePolicy' at 'policyArn'
failed to satisfy constraint:
Member must have length greater
than or equal to 20 (Service:
AmazonIdentityManagement;
Status Code: 400; Error Code: ValidationError;
How can I fix this error? Do I have to specify 'policyArn' manually?
My template.yaml:
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: >
test-function
Resources:
TestFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: test-function/
Handler: app.lambdaHandler
Runtime: nodejs16.x
MemorySize: 512
Timeout: 60
Policies:
- AWSLambdaBasicExecutionRole
- AWSLambdaVPCAccessExecutionRole
- EC2DescribePolicy
Architectures:
- x86_64
Events:
ScheduledEvent:
Type: Schedule
Properties:
Schedule: cron(0 8 * * ? *)
Enabled: True
Version information:
$ sam --version
SAM CLI, version 1.60.0
$ node --version
v16.17.0
$ docker --version
Docker version 20.10.17, build 100c701
$ python3 --version
Python 3.9.6
The documentation states:
For every policy template you specify in your AWS SAM template file, you must always specify an object containing the policy template's placeholder values. If a policy template does not require any placeholder values, you must specify an empty object.
So you should have:
Policies:
- AWSLambdaBasicExecutionRole
- AWSLambdaVPCAccessExecutionRole
- EC2DescribePolicy : {}
I am trying to use the AWS SAM framework to create a lambda to be used as a CloudFront event handler. It seems like the AWS::Serverless::Function doesn't support the Version attribute. The error I am seeing:
com.amazonaws.services.cloudfront.model.InvalidLambdaFunctionAssociationException: The function ARN must reference a specific function version. (The ARN must end with the version number.)
I found this answer that led me to try it. The relevant parts of my CloudFormation YAML file:
Resources:
CloudFrontFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: cloudfront-handler/hello_world/
Handler: app.lambda_handler
Runtime: python3.7
Outputs:
CloudFrontFunctionArn:
Description: CloudFront Function ARN with Version
Value: !Join [':', [!GetAtt CloudFrontFunction.Arn, !GetAtt CloudFrontFunction.Version]]
When I sam deploy I get the following error.
Waiting for changeset to be created..
Error: Failed to create changeset for the stack: my-sam-app, ex: Waiter ChangeSetCreateComplete failed: Waiter encountered a terminal failure state Status: FAILED. Reason: Template error: resource CloudFrontFunction does not support attribute type Version in Fn::GetAtt
The properties available on AWS::Lambda::Function are documented here and it lists Version as one of the properties. So it seems AWS::Serverless::Function doesn't support getting the version. How can I get around this so I can deploy a CloudFront event handler implemented using the AWS SAM framework?
UPDATE
As per #mokugo-devops (thanks!), the fix for this was to add AutoPublishAlias: live like this:
Resources:
CloudFrontFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: cloudfront-handler/hello_world/
Handler: app.lambda_handler
Runtime: python3.7
AutoPublishAlias: live
Outputs:
CloudFrontFunctionVersion:
Description: CloudFront Function ARN with Version
Value: !Ref CloudFrontFunction.Version
By default the function will not automatically have a Version deployed. Instead you will need to specify the AutoPublishAlias attribute.
More information available here.
By doing this you will be unable to retrieve the version.
Instead you will need to create a resource of AWS::Lambda::Version and pass in the Arn from the CloudFrontFunction resource. Then you can get the Lambda version arn from this new resource and pass that to your CloudFrontFunctionArn output.
I am trying to create a Lambda which is invoked when there are records in a Kinesis Stream. For this, in the template.yaml for the lambda, I have added a Kinesis Consumer in the following way -
EventStreamConsumer:
Type: AWS::Kinesis::StreamConsumer
Properties:
StreamARN: !Sub arn:aws:kinesis:${AWS::Region}:${AWS::AccountId}:stream/${EventsKinesisStream}
ConsumerName: !Ref KinesisConsumerName
EventSourceMapping:
Type: 'AWS::Lambda::EventSourceMapping'
Properties:
BatchSize: 100
MaximumBatchingWindowInSeconds: 15
Enabled: true
EventSourceArn: !Ref EventStreamConsumer
FunctionName: !GetAtt Function.Arn
StartingPosition: LATEST
When I deploy this template using the SAM CLI, I see errors saying -
Unsupported MaximumBatchingWindowInSecond parameter for given event source mapping type. (Service: AWSLambda; Status Code: 400; Error Code: InvalidParameterValueException; Request ID: xxxxxxxxxx)
Observe the Error message for the parameter name.
Can someone explain why this is occurring and how this can be mitigated?
Batch window, error handling, and concurrency settings are not available for HTTP/2 stream consumers.
To understand completely you can refer the link below.
Using AWS Lambda with Amazon Kinesis
What you are trying to do will be supported with HTTP/1 not with HTTP/2.
My lambda function/code resides in a S3 bucket.
The lambda function has a piece of code which looks like:
RoleArn = "XXXX:accountid"
The RoleArn will change every time I deploy to a different child account.
I am using a Cloudformation template which has:
FunctiontToUse:
Type: AWS::Lambda::Function
Properties:
Code:
S3Bucket: 'bucket-name'
S3Key: 's3key.zip'
Description: Description
FunctionName: FunctionName
Handler: FunctionName.lambda_handler
MemorySize: 1024
Role: role
Runtime: python3.6
Timeout: 5
If I try using "${accountid}" it doesn;t work. But this works when I paste the function in the CF template and don't use the bucket.
Please advise.
PS: unable to format the cf template above.
So I'm trying to convert an existing spring boot application to an AWS lambda and using SAM.
I'm trying to use aws-sam-cli to try my lambda locally, however with my SAM setup I am getting: Template does not have any APIs connected to Lambda functions
When I do: sam local start-api
My template.yml:
AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: foo
Resources:
MailFunction:
Type: AWS::Serverless::Function
Properties:
Handler: bar.LambdaHandler::handleRequest
Runtime: java8
CodeUri: target/foo-bar-1.0.jar
Timeout: 300
MemorySize: 1024
Events:
Timer:
Type: Schedule
Properties:
Schedule: rate(1 day)
Any idea what I'm doing wrong? It looks correct as far as I can tell from https://blog.couchbase.com/aws-serverless-lambda-scheduled-events-tweets-couchbase/ + https://docs.aws.amazon.com/lambda/latest/dg/tutorial-scheduled-events-schedule-expressions.html
You didn't add any API Gateway event to your function. And start-api spawn a local API Gateway.
You need to add at least one Api event to your Events section.
Events:
[...]
Api:
Type: Api
Properties:
Path: /myresource
Method: get
If you just have a Schedule event, try to use generate-event to create such an event.
sam local generate-event schedule ...
and invoke function e.g. sam local invoke function-name -e event_file.json (see)
For Googlers:
Check whether you have an Event with Type: Api
ALSO check whether you have run sam build (very important)
Use the --debug flag so you will know what is going on
As of 2020/7/13, Type: HttpApi does not work with sam local start-api. See issue.
This error message also displays if you are trying to test a websocket API locally. Unfortunately, local testing of websockets is not currently supported - see https://github.com/awslabs/aws-sam-cli/issues/896.
I ran into this error too even when I did have an Api event defined in my SAM template. The problem was that I had a previous template in my .aws-sam/build/ directory which didn't have the Api event defined (from a previous run of sam build). Cleaning out the build directory fixed it.
I am getting this error, but I have function that is working with the HttpApi, it appears that current version of sam does not support HttpApi.
CLI Version
SAM CLI, version 0.52.0
Example Function
FeedsFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri:
Description: "Function that handles feeds"
Events:
Handler:
Type: HttpApi
Properties:
ApiId: !Ref FeedsApi
Path: /
Method: get
Handler: api
MemorySize: 1024
Runtime: go1.x
Timeout: 5
Tracing: Active
There is currently an open issue on GitHub for adding support: https://github.com/awslabs/aws-sam-cli/issues/1641
I got this error when I had a whitespace error in my AWS::Serverless::Function definition, specifically Environment needed to be a child of Properties but was on the same level. Correcting the whitespace made this error disappear. Nodejs 10.15.