简体   繁体   English

通过 Shell 脚本创建 Route53 记录集

[英]Creating Route53 Record Sets via Shell Script

As a part of my shell script, I am trying to create record sets in AWS Route53.作为我的 shell 脚本的一部分,我正在尝试在 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 .但是,在我的 shell 脚本中使用 aws cli 中的变量来创建这些记录集,我在 Shell 脚本中导出的变量没有传递到aws cli 命令

AWS CLI command provided by AWS: AWS 提供的 AWS CLI 命令:

$ 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.为了简单起见,我不想在我的计算机上创建一个单独的 json 文件,我想将我的所有命令和变量都包含在 shell 脚本中。

#!/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:最后一条命令在 AWS Route53 上创建一个记录集:

$(TARGET_ENVIRONMENT).company.com

with CNAME as以 CNAME 作为

$(BASE_ENVIRONMENT_DNS)

and NOT really as I want it to be, which is:不是我想要的那样,它是:

uat.company.com

with the CNAME:使用 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?如何将环境变量传递到脚本中的 aws cli 命令中?

Any help will be highly appreciated.任何帮助将不胜感激。

Thanks!谢谢!

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"'" } ] } } ] }'

You could also leverage process substitution to keep the JSON formatted:您还可以利用流程替换来保持 JSON 格式:

#!/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
)

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'"
        }]
      }
    }]
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM