AWS Codepipeline Example - amazon-web-services

I am trying the example of Codepipeline for Lambda Function given by AWS. On executing the pipeline its getting failed during cloudformation execution with error saying "Template configuration is not valid"
Below is the template I copied from the example but its failing any suggestions?
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Outputs the time
Resources:
TimeFunction:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs6.10
CodeUri: ./
Events:
MyTimeApi:
Type: Api
Properties:
Path: /TimeResource
Method: GET
Also please let me know where could I see the detailed error log?

Related

SAM CLI - Unable to build

I have a project set up for SAM that I cannot assume was correctly set up.
When I try to run sam build --use-container, this happens:
>sam build --use-container
Starting Build inside a container
Building codeuri: /...whatever.../hello_world runtime: python3.7 metadata: {} functions: ['HelloWorldFunction']
Fetching public.ecr.aws/sam/build-python3.7:latest Docker container image......
Mounting /...whatever.../hello_world as /tmp/samcli/source:ro,delegated inside runtime container
Build Failed
Running PythonPipBuilder:ResolveDependencies
Error: PythonPipBuilder:ResolveDependencies - Requirements file not found: /tmp/samcli/source/requirements.txt
I'm not surprised that it can't find /tmp/samcli/source/requirements.txt, because it does not exist. Instead, I would expect it to look in ./main/requirements.txt, which is there.
Here is my template file:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
my-service
Globals:
Function:
Timeout: 3
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: hello_world/
Handler: app.lambda_handler
Runtime: python3.7
Events:
HelloWorld:
Type: Api
Properties:
Path: /hello
Method: get
Outputs:
HelloWorldApi:
Description: "API Gateway endpoint URL for Prod stage for Hello World function"
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
HelloWorldFunction:
Description: "Hello World Lambda Function ARN"
Value: !GetAtt HelloWorldFunction.Arn
HelloWorldFunctionIamRole:
Description: "Implicit IAM Role created for Hello World function"
Value: !GetAtt HelloWorldFunctionRole.Arn
What could be the reason for this error?
The location of requirements.txt is based on the CodeUri location in the template.
Based on your template, sam build command is expecting it under hello_world/ directory.
Since it cannot find in that location, it's looking under the mounted directory /tmp/samcli/source
Provide requirements.txt in the location specified in CodeUri (hello_world/) in the template to resolve the issue.

Cloudformation LayerVersion S3Bucket Using Parameter

I'm trying to use Cloudformation to package and deploy a simple "hello world" serverless app that uses a single Lambda Layer. The issue I'm having is that the LayerVersion section in my CF template file doesn't seem to like the fact that I'm using a !Ref to specify the S3Bucket and S3Key values. I don't want to hard-code these; nothing I've found in the documentation suggests that what I'm trying to do won't work, but it doesn't work :(
Here's the output of the deploy command that's failing:
aws cloudformation deploy --template-file out.yml --stack-name cftest-lambda --parameter-overrides S3BucketNameParameter=cftest-0eddf3f0b289f2c2 S3LambdaLayerNameParameter=cftest-lambda-layer-1602434332.zip --capabilities CAPABILITY_NAMED_IAM
Waiting for changeset to be created..
Failed to create the changeset: Waiter ChangeSetCreateComplete failed: Waiter encountered a terminal failure state Status: FAILED. Reason: Transform AWS::Serverless-2016-10-31 failed with: Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [libs] is invalid. property Content not defined for resource of type AWS::Serverless::LayerVersion
Here is the full CF template file:
cat template.yml
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: An AWS Lambda application
Parameters:
S3BucketNameParameter:
Type: String
Description: Bucket name for deployment artifacts
S3LambdaLayerNameParameter:
Type: String
Description: Object name for lambda layer deployment artifact
Resources:
helloworldfunction:
Type: AWS::Serverless::Function
Properties:
Handler: lambda_function.lambda_handler
Runtime: python3.8
CodeUri: hello-world-with-layer/.
Description: Hello world function to test cf using layers
Timeout: 10
# Function's execution role
Policies:
- AWSLambdaBasicExecutionRole
- AWSLambdaReadOnlyAccess
- AWSXrayWriteOnlyAccess
Tracing: Active
Layers:
- !Ref libs
libs:
Type: AWS::Serverless::LayerVersion
Properties:
Content:
S3Bucket: !Ref S3BucketNameParameter
S3Key: !Ref S3LambdaLayerNameParameter
CompatibleRuntimes:
- python3.8
LayerName: hello-world-lib
Description: Dependencies for the hello-world-with-layer app.
Any suggestions on how to approach this correctly?
The correct properties for LayerContent are:
Bucket: String
Key: String
Version: String
However, you are using (different names):
S3Bucket: String
S3Key: String

How to read aws sam parameter in .NET lambda function

I have a SAM template that takes parameters in the .yml file, and I want to access the parameter value within the lambda function written in c#
here is the SAM template
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
Globals:
Function:
Timeout: 60
Runtime: dotnetcore2.1
Parameters:
ApiBaseUri:
Type: String
Default: https://postb.in/1584332032935-8906962152104
Description: The API URL where the received events will be pushed by the lambda function
Resources:
PushUpdates:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./src/PushAPIs/
Handler: Localgov.Microservices::LambPaymentTracker.UpdateHandler::PushBurtonUpdates
Runtime: dotnetcore2.1
Policies: AWSLambdaDynamoDBExecutionRole
Events:
Stream:
Type: DynamoDB
Properties:
Stream: !GetAtt UpdatesTable.StreamArn
BatchSize: 100
StartingPosition: TRIM_HORIZON
The common way of passing cloudformation (or sam) variables to lambda would be through Environment Variables.
In your template you specify the variables using Environment property.
In your code, you then use your programming language specific functionality to read them.

How to create 2 AWS lambdas on a single CloudFormation stack?

I currently have 2 lambda functions, and I'm trying to make a CI/CD process to them. So I have trying with 2 approaches:
two separate steps on my CI. I have tried to make CloudFormation package and then deploy each lambda, with each of those having their own SAM template and template. but the result is that the only one that will remain on the stack is the last one deployed. i understand that deploy is an smart way that AWS CLI create to do not use create/update stack actions. but it keeps overwriting between them (yes they have different resource name).
having a single sam template and one step in a single repo: I also attempt this in a single repo having both lambdas and a single sam file, but I have duplicate code on my lambdas the difference is that each of them have different set up for which handler to be used.
My goal is to have 2 lambdas in a single stack.
im going to answer my own question because I notice on the sam template was the key.
initially i was doing the sam template like this:
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: An AWS Serverless Specification template describing your function.
Resources:
certainty:
Type: 'AWS::Serverless::Function'
Properties:
Handler: ./myfunc/index.handler
Runtime: nodejs8.10
CodeUri: .
Description: >-
here goes
my description
MemorySize: 128
Timeout: 20
Role: 'arn:aws:iam::116738426468:role/rolename'
Events:
Schedule1:
Type: Schedule
Properties:
Schedule: rate(1 day)
certaintyauxiliar:
Type: 'AWS::Serverless::Function'
Properties:
Handler: my-other-func/index.handler
Runtime: nodejs8.10
CodeUri: .
Description: >-
blabla
blabla.
MemorySize: 1152
Timeout: 300
Role: 'arn:aws:iam::116738426468:role/roleanme'
Events:
Api1:
Type: Api
Properties:
Path: /show-all
Method: POST
what was causing here a "duplicate of the code" was that the lambdas code uri was indicating that should grab all the stuff in the folder that cotained both repos. and telling to go deeper in directories to find the the handler.
so I changed the code uri and the handler and now the lambdas are grabbing just what should be in each lambda. now my sam template looks like this:
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: An AWS Serverless Specification template describing your function.
Resources:
certainty:
Type: 'AWS::Serverless::Function'
Properties:
Handler: index.handler
Runtime: nodejs8.10
CodeUri: ./my-func
Description: >-
here goes
my description
MemorySize: 128
Timeout: 20
Role: 'arn:aws:iam::116738426468:role/roleName'
Events:
Schedule1:
Type: Schedule
Properties:
Schedule: rate(1 day)
certaintyauxiliar:
Type: 'AWS::Serverless::Function'
Properties:
Handler: index.handler
Runtime: nodejs8.10
CodeUri: ./my-other-func
Description: >-
bla bla
bla bla
MemorySize: 1152
Timeout: 300
Role: 'arn:aws:iam::116738426468:role/rolename'
Events:
Api1:
Type: Api
Properties:
Path: /path
Method: POST
sorry, now i can see that at the question I was not providing enough info, but i answer to my own question hoping I can help some as lost as I was. Serverless is a nice approach but it does have quiet a learning curves.
Regards, Daniel

AWS SAM - How to specify the name of your function

I'm trying the Create Your Own Serverless Application and I see that the name of the function is specified in the YAML template but when it gets deployed it creates a lambda with a composite name based on:
CloudFormation stack + Lambda function + Some Id.
My questions is: Is there a way to override the name of the function when using AWS SAM?
Thanks
Yes there is, take a look at https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
You need the FunctionName parameter in your YAML.
Similar to the following:
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Resources:
samPocFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: samPoc
Description: This is my SAM POC function
Runtime: python2.7
CodeUri: ./functions/mycode
Handler: handler.handler
MemorySize: 128
Timeout: 3