I have an independent lambda layer, the arn is retrieved using the below CLI command.
aws lambda list-layer-versions --layer-name my-custom-lambda-layer --region us-east-1 --query 'LayerVersions[0].LayerVersionArn'
How can I refer this output to my cloud formation template, like below,
Resources:
Parameters:
MYLAYERARN: $(aws lambda list-layer-versions --layer-name my-custom-lambda-layer --region us-east-1 --query 'LayerVersions[0].LayerVersionArn')
Or use it directly in any of my lambda function as below,
Resources:
MyLambdaFuntion:
handler: Hello.lambda_handler
timeout: 60
memorySize: 256
layers:
- $(aws lambda list-layer-versions --layer-name my-custom-lambda-layer --region us-east-1 --query 'LayerVersions[0].LayerVersionArn')
Currenlty it is not executing the AWS CLI command, but taking the CLI command as the value
That is not possible, as you can't evaluate such expressions in a CloudFormation template.
The easiest solution would be to pass in the already-evaluated expression as a parameter.
Alternatively, if you must use a CloudFormation solution, then you could leverage a CloudFormation macro to invoke a lambda function which executes custom code (in this case, the code would have the SDK equivalent of the AWS CLI command).
More on CloudFormation macros:
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-macros.html
Macro example: https://stackoverflow.com/a/70475459/3390419
Related
This page describes how to set a stack name in some AWS console GUI: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-using-console-create-stack-parameters.html
How do I set these values in the SAM Template .yml files?
I'm specifically doing this on a Stack that is only a Lambda Layer if that matters.
I can see that there is some way to do this via CLI as described here:
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-cli-creating-stack.html
aws cloudformation create-stack --stack-name myteststack --template-url "ssm-doc://arn:aws:ssm:us-east-1:123456789012:document/documentName"
Is it even possible to set the name in the template?
Unfortunately, it seems like stack name is NOT part of the SAM templates. This is done via the command arguments to deploy the stack.
From the same link: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-cli-creating-stack.html
The following example creates the myteststack stack in an Amazon S3 bucket:
PROMPT> aws cloudformation create-stack \
--stack-name myteststack \
--template-body file:///home/testuser/mytemplate.json \
--parameters ParameterKey=Parm1,ParameterValue=test1 ParameterKey=Parm2,ParameterValue=test2
So when creating the stack, the --stack-name argument is how this is set.
The reason I was confused is because I didn't realize where that command was being issued.
I am using the AWS CLI and CloudFormation to create a new S3 bucket.
Here is my yaml file:
AWSTemplateFormatVersion: '2010-09-09'
Description: Creates an S3 bucket
Parameters:
BucketName:
Description: Name of the Bucket
Type: String
Resources:
ArtifactBucket:
Type: AWS::S3::Bucket
DeletionPolicy: Retain
Outputs:
ArtifactBucket:
Value: !Sub ${BucketName}
BucketArn:
Value: !GetAtt ArtifactBucket.Arn
Description: Arn of the new bucket
I run it with the following cli command in a terminal window:
aws cloudformation deploy --stack-name brendan-s3 \
--template-file ComposeEveryApp/create-s3-bucket.yaml \
--profile compose-staging \
--parameter-overrides BucketName=brendan
Everything works fine. Here is the new bucket displayed in the AWS console:
I'd like to display the Arn of the new bucket (as shown above) in the terminal window. How do I do that?
The command aws cloudformation deploy is only instructing the CloudFormation service to start the deployment and not actually waiting for the deployment to finish. Hence there is no link between the Outputs section and the return value of the command you're executing on the CLI.
If you want the Outputs of a cloudformation stack, you'll have to use the describe-stacks command, and you'll need to combine it with a client side filter using --query if you want to only output that specific value.
You can find more info on this SO question.
You can use describe-stacks command. One of its return values will be outputs of your stack.
Am i missing something ? it seems that you can use --profile with almost any other aws cli functionality.
is there any other way around this then by manually running aws configure ?
update Lambda environment variables from JSON file
aws lambda update-function-configuration --profile mfa --function-name test-api --cli-input-json file://dev.json
I have a functional stack that works on the console but has issues when I try to run it in the CLI. This is what happens:
[user#lsikala1 ~]$ aws cloudformation create-stack --stack-name G2Devopsproject --template-body https://s3.amazonaws.com/g2internship2018/G2InternshipDevopsbuild.json
An error occurred (ValidationError) when calling the CreateStack operation: Parameters: [KeyName, Subnets, DBPassword, VpcId, DBUser] must have values
The AWS CLI won't prompt you for parameters when creating CloudFormation stacks. Instead you'll have to provide them using the --parameters parameter. From the reference of the create-stack command:
--parameters (list)
A list of Parameter structures that specify input parameters for the
stack. […]
Of course you only have to specify parameter values without a default in your CloudFormation template.
I just want to get back a list of function names. Ideally I want to get all functions (just their name) starting with "some-prefix*". Can I do this with the cli?
Really want this as a cli command if possible (I want to avoid python or another sdk). I see there is a --cli-input-json arg, can I use that for filtering?
You can do that. Use the --query option. The CLI would look like this:
aws lambda list-functions --region us-east-1 --query 'Functions[].FunctionName' --output text
To get the list of functions whose name begin with some-prefix:
aws lambda list-functions --region us-east-1 --query 'Functions[?starts_with(FunctionName, `some-prefix`) == `true`].FunctionName' --output text
To get the complete JSON, the CLI would be:
aws lambda list-functions --region us-east-1
Details about the query parameter can be found here.
As the answer is already given by #krishna, but I was looking for a way to print all function name without specifying a prefix. So here you can get all lambda function name in particular region my default is us-west-2.
aws lambda list-functions --query 'Functions[*].[FunctionName]'
Or as I want them out in text format and space separated to use in my bash script so here you can get in text and single line space separated.
aws lambda list-functions --query 'Functions[*].[FunctionName]' --output text | tr '\r\n' ' '
I have come here for some help to clean up all lambda functions that I have created while following an AWS developer certification tutorial. If anyone is in the same boat, I have created a script to programmatically delete all lambda functions in my AWS account (NOT for production use)
#!/bin/bash
# STOP: DON'T USE/RUN THIS SCRIPT UNLESS YOU ARE 100% SURE WHAT YOU ARE DOING
# I am a learner and created 20+ lambda functions while following a tutorial
# I wrote this script to programatically cleanup functions at the end of course
# precondition: your aws cli is configured
# get all functions from aws account
functions=(`aws lambda list-functions --query 'Functions[*].[FunctionName]' --output text`)
for i in "${functions[#]}"
do
#delete functions 1-by-1
aws lambda delete-function --function-name "$i"
echo "deleted $i"
done
Incase, someone is looking for a similar query with string present in the lambda function name as a substring, try below
aws lambda list-functions --region us-east-1 --query 'FunctionName[?contains(FunctionName, 'containing-string'] == 'true'].[FunctionName]' --output text
Note - the '[]' brackets around '.FunctionName' will provide with each fucntionName on a new line.
You can easily get the list of all lambda functions in given region using below command:
aws lambda list-functions --region us-east-1 | jq -r .Functions[].FunctionName
Download the jq (Lightweight and flexible command-line JSON processor) from here:
https://stedolan.github.io/jq/download/