Creating Route53 Record Sets via Shell Script
Asked Answered
L

4

18

As a part of my shell script, I am trying to create record sets in AWS Route53. However, using variables in the aws cli within my shell script to create those records set, my variables exported in the Shell scripted are not being passed to the aws cli command.

AWS CLI command provided by AWS:

$ aws route53 change-resource-record-sets --hosted-zone-id 1234567890ABC \
  --change-batch file:///path/to/record.json

I do not want to create a separate json file on my computer for simplicity purpose, I want to have all my commands and variables within the shell script.

#!/bin/bash

export TARGET_ENVIRONMENT=uat 
export BASE_ENVIRONMENT_DNS=abcd-External-9982627718-1916763929.us-west-1.elb.amazonaws.com

# Creates route 53 records based on env name

aws route53 change-resource-record-sets --hosted-zone-id 1234567890ABC  
--change-batch '{ "Comment": "Testing creating a record set", 
"Changes": [ { "Action": "CREATE", "ResourceRecordSet": { "Name": 
"$(TARGET_ENVIRONMENT).company.com", "Type": "CNAME", "TTL": 
120, "ResourceRecords": [ { "Value": "$(BASE_ENVIRONMENT_DNS)" } ] } } ] }'

This last command is creating a record set on the AWS Route53 as:

$(TARGET_ENVIRONMENT).company.com

with CNAME as

$(BASE_ENVIRONMENT_DNS)

and NOT really as I want it to be, which is:

uat.company.com

with the CNAME:

abcd-External-9982627718-1916763929.us-west-1.elb.amazonaws.com

How can I pass the environment variables into my aws cli command within the script?

Any help will be highly appreciated.

Thanks!

Lindgren answered 12/3, 2018 at 5:14 Comment(0)
K
26

Variables do not expand within single quotes.

If you close the single quotes before your variable expansion, and then open them again immediately after, it should produce the desired effect. You may or may not need to wrap the variable in double quotes for it to expand.

#!/bin/bash

ENV=uat 
DNS=abcd-External-9982627718-1916763929.us-west-1.elb.amazonaws.com

# Creates route 53 records based on env name

aws route53 change-resource-record-sets \
  --hosted-zone-id 1234567890ABC \
  --change-batch '
  {
    "Comment": "Testing creating a record set"
    ,"Changes": [{
      "Action"              : "CREATE"
      ,"ResourceRecordSet"  : {
        "Name"              : "'" $ENV "'.company.com"
        ,"Type"             : "CNAME"
        ,"TTL"              : 120
        ,"ResourceRecords"  : [{
            "Value"         : "'" $DNS "'"
        }]
      }
    }]
  }
  '
Kleinstein answered 12/3, 2018 at 5:38 Comment(3)
For those who may scratch their heads trying to understand why this works, the shell does not require a concatenation operator for adjacent quoted literals (which are expanded when double quotes are used). echo 'a'"b"'c' illustrates this. (Result: abc with no spaces in between.)Torsi
Thank you @nicholas.hauschild, that worked. Appreciate the help.Lindgren
I hate that this works. But I'm also glad.Balough
R
4

You could also leverage process substitution to keep the JSON formatted:

#!/bin/bash

export TARGET_ENVIRONMENT=uat 
export BASE_ENVIRONMENT_DNS=abcd-External-9982627718-1916763929.us-west-1.elb.amazonaws.com

# Creates route 53 records based on env name

aws route53 change-resource-record-sets --hosted-zone-id 1234567890ABC --change-batch file://<(cat << EOF
{
  "Comment": "Testing creating a record set",
  "Changes": [
    {
      "Action": "CREATE",
      "ResourceRecordSet": {
        "Name": "${TARGET_ENVIRONMENT}.company.com",
        "Type": "CNAME",
        "TTL": 120,
        "ResourceRecords": [
          {
            "Value": "${BASE_ENVIRONMENT_DNS}"
          }
        ]
      }
    }
  ]
}
EOF
)
Randolph answered 29/9, 2021 at 7:18 Comment(1)
CAUTION! This does not work if your record is too long!!Chlor
N
3

This worked for me!

#!/bin/bash
zoneid=xxxxyyyyyzzzzz
recordname=mycname
recordvalue=myendpoint

aws route53 change-resource-record-sets \
  --hosted-zone-id $zoneid \
  --change-batch '
  {
    "Comment": "Creating a record set for cognito endpoint"
    ,"Changes": [{
      "Action"              : "CREATE"
      ,"ResourceRecordSet"  : {
        "Name"              : "'$recordname'.mydomain.com"
        ,"Type"             : "CNAME"
        ,"TTL"              : 120
        ,"ResourceRecords"  : [{
            "Value"         : "'$recordvalue'"
        }]
      }
    }]
  }
Newsworthy answered 24/11, 2022 at 10:38 Comment(1)
This works with GNU bash, version 4.2.46(2)-release (x86_64-koji-linux-gnu)Flask
U
0

You need to close the single quotation mark. Here is the fixed snippet.

#!/bin/bash
zoneid=xxxxyyyyyzzzzz
recordname=mycname
recordvalue=myendpoint

aws route53 change-resource-record-sets \
  --hosted-zone-id $zoneid \
  --change-batch '
  {
    "Comment": "Creating a record set for cognito endpoint"
    ,"Changes": [{
      "Action"              : "CREATE"
      ,"ResourceRecordSet"  : {
        "Name"              : "'$recordname'.mydomain.com"
        ,"Type"             : "CNAME"
        ,"TTL"              : 120
        ,"ResourceRecords"  : [{
            "Value"         : "'$recordvalue'"
        }]
      }
    }]
  }'
Unreality answered 20/2, 2023 at 10:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.