Passing a relative file path through aws cloudformation deploy parameter-overrides - amazon-web-services

I am working on adding Dynatrace monitoring as a layer to a Lambda serverless function in a nested stack. The environment variables needed for the Dynatrace layer are being passed from the root stack to the serverless function stack where they are received in a Globals block.
One of the needed environment variables is AWS_LAMBDA_EXEC_WRAPPER, and I am trying to pass a relative path of /opt/dynatrace.
I am developing and testing using bash shell scripts on my workstation, so I am using a script to execute the aws cloudformation deploy command and passing parameter overrides for the Dynatrace monitoring. The variable in question is using pDtLambdaExecWrapper=/opt/dynatrace.
When passing the value as /opt/dynatrace, the path is being parsed as C:/Program Files/Git/opt/dynatrace, so gives an error that it does not exist.
I tried passing the value as ./opt/dynatrace, but instead get an error, ./opt/dynatrace: does not exist.
I am using a Windows 10 workstation, and using Git Bash for my terminal shell emulator.
Is there a way to pass in a relative file path from the AWS command line using the parameter-overrides option on the aws cloudformation deploy command in a way that will pass just /opt/dynatrace?
Deploy command below. I've removed or changed some of the other parameters being passed in to save space.
aws --region $REGION cloudformation deploy \
--template-file $OUTPUT_TEMPLATE_FILE \
--stack-name $STACK_NAME \
--no-fail-on-empty-changeset \
--capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM CAPABILITY_AUTO_EXPAND \
--parameter-overrides \
pDtTenant=26571e7e-xxxx-4fed-a826-67b899abfee8 \
pDtClusterId=-nnnnnnnnnn \
pDtLambdaExecWrapper=/opt/dynatrace \
pDtLambdaLayerArn=arn:aws:lambda:us-east-1:xxx:1

Related

How do I set an AWS Stack name (for a Lambda Layer) in a SAM Template?

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.

Difference between --parameter-overrides and --tags

I am having a hard time understanding the difference between --parameter-overrides and --tags when deploying an AWS stack using CloudFormation.
I tried to read through the documentation but I still do not understand, I seem to get exactly the same behaviour when I use the cli to deploy my stack with the usage of one or the other flag, such as
aws --profile $PROFILE cloudformation deploy
--stack-name ${STACK_NAME}
--template-file $TEMPLATE_FILE
--parameter-overrides
ApplicationName=$APP_NAME
--no-fail-on-empty-changeset
--tags
ApplicationName=$APP_NAME
When and why would I use the tags? Any help?
--tags set arbitrary Tags on the Stack. Tags are key-value metadata for labelling and categorizing AWS resources. Tags are optional. They do not affect how CloudFormation deploys the stack.
--parameter-overrides inject parameter values into the template. Optional if you are happy with the template's parameter defaults (for new deploys) or currently deployed values (for updates).

How can I resolve non supported format error in aws cloudformation script

I am writing a cloudformation script which is supposed to pick the stack name, template body and parameter file from a .txt file for deployment. I would not want the yaml and json file to be edited during a new deployment instead the .txt file should be edited
The code is below
aws cloudformation create-stack --stack-name $(<stack_name.txt) --template-body
file://$(<stack_template_file_name.txt) --parameters file://$(<stack_parameter_file_name.txt) capabilities "CAPABILITY_IAM" "CAPABILITY_NAMED_IAM" --region=us-west-2
note: stack_name.txt contains the name to be used the stack, stack_template_file_name.txt contains the name of the template.yml file, stack_parameter_file_name.txt contains the name of the parameter.json file
when I type the command directly in the cli, the stack is deployed but when I copy it into create.sh and run ./create.sh I get the error below
`' doesn't match a supported format.`
How can I fix this?
I just wanted to comment I found the same error from a different issue in the aws CLI.
' doesn't match a supported format.
When running:
aws s3 cp file.txt s3://bucket-name/
The issue was a malformed configuration in the
~/.aws config folder.
Re-running the
~/.aws configure command didn't seem to fix the issue
however by inspecting the files in the
~/.aws folder I was able to remove the random characters that were causing the issue.
In my case I was using Ubuntu Windows Linux Subsystem.
I was getting shell variables from properties file.
For e.g. My properties file abc.properties.
#VSRX
region=us-west-1
key_name=cft111
key_location=/var/cft111.pem
My Shell script abc.sh
#!/bin/bash
action=$1
source ./abc.properties
if [[ $action == "create" ]]; then
aws cloudformation deploy \
--template-file abc.yml \
--stack-name ${parent_stack_name} \
--capabilities CAPABILITY_NAMED_IAM \
--region ${region} \
--parameter-overrides KeyName=${key_name}
fi
I faced the similar issue and i tried echoing the command as well and i saw region parameter itself were invisible in echoed command.
To my resolution : it was only formatting issue and i fixed it with below two commands.
sed -i 's/\r$//' abc.sh
sed -i 's/\r$//' abc.properties
I found out that it had to do with the EC2 environment, the exact command worked in a Windows and Ubuntu system.

cloudformation deploy capability_auto_expand

I'm attempting to create a script to automate the deployment/updating of my SAM stack. I'm using batch to do this. Right now I have:
call aws cloudformation deploy --template-file "serverless.yml" ^
--stack-name %1 ^
--capabilities CAPABILITY_AUTO_EXPAND CAPABILITY_IAM^
--parameter-overrides ^
StageName=%1^
{some other parameters}
--role-arn {my role arn}
where %1 is the batch argument with the stack/stage name.
When i attempt to run this, I get the error in the cloudformation console that I need CAPABILITY_AUTO_EXPAND to update some of my stacks. Looking at the documentation, it looks like aws cloudformation deploy does not support this capability? And aws cloudformation update-stack does not accept a filename for a template.
Any suggestions on how to do this?
probably you should use clean formatting
aws cloudformation deploy \
--region "${region}" \
--template-file output.yaml \
--stack-name "${stackName}" \
--capabilities CAPABILITY_IAM CAPABILITY_AUTO_EXPAND \
--parameter-overrides ;
See answer provided by similar question and the answer relevant to the cli commands.
Sorry would have used comment instead of answer but don't yet have privileges.
After switching to sam deploy I figured out that it was an issue with the spaces before and after my line breaks.
Specifically I think I had too many spaces in
--stack-name %1{space}{space}{space}^{space}
then no space after the capabilities line. Cleaned up the spacing throughout the command and it works now
I believe that the biggest issue was the space after the ^

How can I update my AWS Lambda function from VSCode?

So I have an AWS Lambda function written in NodeJS, but I am tired of coding in the AWS Console or having to manually zip my code in my VSCode to manually upload it in he AWS Console.
I know that I can update my function with aws lambda update-function-code --function-name myFunction --zip-file "fileb://myZipFile". But how can I zip it and launch this command every time I save my work in VSCode ?
Also, I am on Windows.
You can't do this without some additional work.
A few options are:
use the Run on Save VS Code extension and configure a custom command to run when a file is saved
create a SAM project and install the AWS Toolkit for VS Code extension to provide deployment assistance
create a package.json that includes a script for zip/deployment and use the NPM extension for VS Code to execute the deploy script
build a CI/CD solution: use VS Code to commit and push your code, then the pipeline takes over and deploys it
use a shell script, or a Makefile with a target, that zips and deploys and then simply execute it, manually or otherwise, in the VS Code in-built terminal
I use a script with below and run it when need to update.
echo "Building zip file"
zip -rq testfunction.zip testfunctionfolder/
echo "update Lambda function"
FUNCTION_ARN=$(aws lambda update-function-code \
--function-name testfunction \
--zip-file fileb://testfunction.zip \
--query 'FunctionArn' \
--output text)
echo "Lambda function updated with ARN ${FUNCTION_ARN}"