if creating a couple of CloudFormation (cf) skripts and run them in a whole bash-script. How to get output from cf before as input for next cf?

One script runs like this:

aws cloudformation deploy --template-file myTemplate.yaml --stack-name myStackName

Or if need package before like this

aws cloudformation package --template-file template.yaml --s3-bucket my-S3-bucket --output-template-file output.yaml

aws cloudformation deploy --template-file output.yaml.yaml --stack-name myStackName

Override a parameter.

aws cloudformation deploy --template-file output.yaml.yaml --stack-name myStackName --parameter-overrides AppName='my First App' DevAccountId=23456 TestAccountId=98765

Creating bash/shell script for more than one cf is here

#!/usr/bin/env bash
AppName='my First App'
DevAccount=123456
TestAccount=98765
aws cloudformation deploy --template-file output1.yaml.yaml --stack-name myFirstStack --parameter-overrides AppName=$AppName DevAccountId=$DevAccount TestAccountId=$TestAccount
aws cloudformation deploy --template-file output2.yaml.yaml --stack-name mySecondStack --parameter-overrides AppName=$AppName DevAccountId=$DevAccount TestAccountId=$TestAccount
aws cloudformation deploy --template-file output3.yaml.yaml --stack-name myThirdStack --parameter-overrides AppName=$AppName DevAccountId=$DevAccount TestAccountId=$TestAccount

And now!

How to combine them? How to use export (not export, only output) from one stack as input to another stack, only using bash?

Getting output, use describe-stacks like this

aws cloudformation describe-stacks --stack-name myStackName --output text

Or with –output json or table, but you got whole stackdescription.

Reduce to output-only with this: All outputs

aws cloudformation describe-stacks --stack-name thomas-is-doph --query "Stacks[0].Outputs[].OutputValue"  --output text

or this: one specific output (Outputs[0] or Outputs[1] )

aws cloudformation describe-stacks --stack-name thomas-is-doph --query "Stacks[0].Outputs[0].OutputValue"  --output text 

You have to know the order of outputs!

But it gets better!

You can get a specific output with

aws cloudformation describe-stacks --stack-name myStackName --query "Stacks[0].Outputs[?OutputKey=='SearchTerm'].OutputValue" --output text

Now you get directly your searchTerm!

If you combine them in a script, a function would be nice

try this:

function getOutput { echo -n "function called to find $1" queryParameter="Stacks[0].Outputs[?OutputKey=='$1'].OutputValue" echo -n "Query Paramter= $queryParameter" outputVar='aws cloudformation describe-stacks --stack-name $2 --query $queryParameter - --output text' 
result=$(eval "$outputVar") 
echo "searching variable:> $result"}

In a whole script it looks like this:

#!/usr/bin/env bash

##################################################
## Getting output from CI! ##
## ##
## Needs 3 parameter ##
## $1 name of Output ##
## $2 name of Stack ##
## result name in mandatory ##
## contains result ##
## ##
## Example: ##
## getOutput KMSKey moe-cloud-stack result ##
##################################################
function getOutput {

  echo -n "function called to find $1"
  queryParameter="Stacks[0].Outputs[?OutputKey=='$1'].OutputValue"
  echo -n "Query Paramter= $queryParameter"
  outputVar='aws cloudformation describe-stacks --stack-name $2 --query $queryParameter --profile moe-ci --output text --region eu-central-1'
  result=$(eval "$outputVar")
  echo "searching variable:> $result"
}

AppName='my First App'
DevAccount=123456
TestAccount=98765
aws cloudformation deploy --template-file output1.yaml.yaml --stack-name myFirstStack --parameter-overrides AppName=$AppName DevAccountId=$DevAccount TestAccountId=$TestAccount

getOutput important myFirstStack result
first=$result

getOutput moreImportant myFirstStack result
second=$result

aws cloudformation deploy --template-file output2.yaml.yaml --stack-name mySecondStack --parameter-overrides AppName=$AppName moreImportant=$second important=$first

very nice, isnt it?

AWS CloudFormation scripting

Thomas Michael


Softwareentwickler aus Leidenschaft.


Beitragsnavigation


Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.