AWS, serverless SAM template - publishing nested applications - amazon-web-services

I have defined AWS Serverless nested applications within my root SAM template by using Location property pointing to my local file system (as advised here - Defining a Nested Application from the Local File System).
Packaging and deploying work perfectly fine - applications run on AWS just fine - everything works just fine, except publish.
I can not find a way to publish to the Serverless Application Repository my root application that would also (somehow) contain all nested applications (inside?).
sam publish \
--template packaged.yaml \
--region us-east-1
returns
Error: Invalid Serverless Application Specification document. Number of errors found: XX. Errors: Resource with id [YYYYYYYYYYY] is invalid. Location property must be an Application Location Object referencing a valid AWS Serverless Application Repository application.
All my Location properties after the packaging is done are something like:
https://s3.eu-east-1.amazonaws.com/my-storage/34ct54v6547b56756n7.template
Does it mean I still need to package or/and publish all of the nested applications first to have CodeUri properties defined as AWS s3 URLS and then somehow change their Locations references in the root packaged template?
Did anyone try that, maybe?

Related

AWS CDK accessing parameters when deploying stacks on the pipeline via yaml, typescript and nodejs

I'm fairly new to AWS and using the CDK but have been working on a project which deploys to AWS via a pipeline using yaml for the cf-template and later a node script to run cdk deploy on a set of stack files that have been written in Typescript.
In the cf-template yaml where the cdk-toolkit is being defined there's a bucket resource with name X. After the toolkit has been created/updated in the pipeline, the cdk deploy command is executed to deploy some stacks and workers, which should live in bucket X. They aren't automatically being uploaded there however, so I've tried using the --parameters flag to specify X as below.
cdk deploy --toolkit-stack-name my-toolkit --parameters uploadBucketName=X --ci --require-approval never
When I do this I get the following error in the pipeline for the first stack that gets deployed:
Parameters: [uploadBucketName] do not exist in the template
I assumed this meant that in the MyFirstStack.ts file it was missing a parameter definition as suggested by the AWS documentation, but it's not clear to me why this is necessary or how it's supposed to be used when it's the cdk deploy command which provides a value for this parameter. I tried adding it per the docs
const uploadBucketName = new CfnParameter(this, "uploadBucketName", {
type: "String",
description: "The name of the Amazon S3 bucket where uploaded files will be stored."});
but not sure if this is really the right thing to do, and it doesn't work besides - I still get the same error.
Does anyone have any ideas where I'm going wrong?

How to use parameter from Gitlab in AWS CDK?

I am using AWS CDK and want to use parameters stored in Gitlab as variables in CDK build.
Someone tried something like this?
Solutions found:
Create custom variable in Gitlab (with the same repository as project) in two ways:
In pipeline file with project (not sensitive data)
In project properties in Gitlab (recomended solution if want to store sensitive params as credentials - those parameters should not be stored with project in file)
Declare custom env in main CDK file (e.x. const CUSTOM_ENV = ${CUSTOM_ENV})
Call cdk deploy with created parameter e.g. cdk deploy $CUSTOM_ENV and the variable will be automatically delivered by Gitlab.
More details how to declare variables - Gitlab docs

How to build CloudFormation template from multiple yml

I'm trying to get this repo going here - https://github.com/mydatastack/google-analytics-to-s3.
A link is provided to launch the AWS CloudFormation stack but it is no longer working as the S3 bucket containing the template is no longer active.
I have 2 questions about getting data pipeline running:
My first question would be what is 631216aef6ab2824fc63572d1d3d5e6c.template and can I create it through the 3 .yml files in the CloudFormation folder?
I've tried to create a template through CloudFormation designer , collector-ga.yml but it fails. I think its because the Resources within the yml aren't available when creating a template just from collector-ga. I've also tried uploading the repo to s3 and creating a template from there but that was also unsuccessful.
How can I launch the stack from the repo? I've found very little information online so an explanation or a pointer to some relevant resources would be appreciated.
This repository doesn't use the "standard" CloudFormation resources, but it uses AWS SAM. You'll have to install the SAM CLI tool and use that to deploy the CloudFormation stack. If you run sam deploy --guided it will help you with the setup of the necessary S3 bucket etc on your AWS account. SAM will upload the necessary files, resolve the internal local links between the templates by updating them with the S3 URLs and construct a packaged.yml template which it will use to deploy the stack.
Also, check out the AWS SAM user guide for more information.

CloudFormation change set chokes on empty CodeUri: in SAM templates

If this is invalid CloudFormation templating then someone should tell the engineer that wrote this blog post: Managing Lambda#Edge and CloudFront deployments by using a CI/CD pipeline | Networking & Content Delivery
I'm using that template to deploy successfully.
But I want to use change sets to have safer deploys.
When I try to create a change set CloudFormation tells me CodeUri is missing:
Transform AWS::Serverless-2016-10-31 failed with: Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [LambdaEdgeFunctionSample] is invalid. Either 'InlineCode' or 'CodeUri' must be set
This template deploys without issue.
Also I don't think I can use the sam commands for this (A simpler deployment experience with AWS SAM CLI | AWS Compute Blog) because the lambda function is only a small part of this CloudFormation template.
When trying a different CodeURI I can see it demands an s3 URL:
Transform AWS::Serverless-2016-10-31 failed with: Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [MyFunction] is invalid. 'CodeUri' is not a valid S3 Uri of the form \"s3://bucket/key\" with optional versionId query parameter.
For the template in the first article, If you remove the line 'CodeUri:' the template should deploy successfully. It seems to be complaining cause the line is empty.
If you are looking for a simple template to kick off a sam project, take a look at this one https://github.com/healthbridgeltd/nodejs-sam-bootstrap
It has a make file with multiple targets that makes your life easier.

How to deploy environment variable using serverless deploy for AWS lambda

I am new to AWS lambda i have i am using serveless deploy to deploy my lambda function declared in serverless.yml file.
In that file i wrote a lambda function i deployed and it is working fine but problem is that whatever environment variable i gave is not available in console of that lambda function. I might be doing some minor mistake or for deploying environment variable there should be some other syntax.
I can go to lambda function in console and add environment variable manually.
But my doubt is can we do it while deploying it through serverless deploy
You can use versions and aliases provided by AWS Lambda
You can create different versions of the same lambda function and give them an alias. Like when you push your lambda code - create a version (say it's 5) - create an alias this (say TEST).
When you're sure that its ready for production, create a version(or choose an existing version and name that (say PROD).
Now whenever your lambda function executes, it gives lambda ARN
which contains alias, by that you can know which alias(in context.invokedFunctionArn) is
executed i.e. that can be used as the environment variable. While
invoking the function, you can mention which function to execute from
your invocation code.
let thisARN = context.invokedFunctionArn;
// Get the last string in ARN - It's either function name or the alias name
let thisAlias = thisARN.split(":").pop();
Now whenever you deploy a new code, just point the alias to that version.
You can use AWS console or CLI for that.
Take a look at this lambda versioning and aliases
For directly deploying to your alias(PROD), you can do this -
aws lambda update-alias \
--region region \
--function-name helloworld \
--function-version 2 \
--name PROD
serverless deploy
Serverless deploy works fine for deployment on any stage it also deploys environment variable in given stage, my case environment variable was not deployed of indentation problem in yaml file, and even serverless deploy command was not throwing error it was deploying function but environment variables were not deployed
In yaml file we can state the the stage where we want to deploy like this
provider:
name: aws
runtime: nodejs6.10
stage: dev
region: eu-west-2
Hope this will help if someone gets similar issue