通过Shell脚本创建Route53记录集

zour9fqk  于 2022-11-25  发布在  Shell
关注(0)|答案(3)|浏览(149)

作为shell脚本的一部分,我尝试在AWS Route 53中创建记录集。但是,在shell脚本中使用aws cli中的变量创建这些记录集时,在Shell脚本中导出的变量未传递到aws cli命令
AWS提供的AWS CLI命令:

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

为了简单起见,我不想在我的计算机上创建一个单独的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)" } ] } } ] }'

最后一个命令将在AWS Route 53上创建一个记录集,如下所示:

$(TARGET_ENVIRONMENT).company.com

CNAME为

$(BASE_ENVIRONMENT_DNS)

但并不是我想要的样子

uat.company.com

使用CNAME:

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

如何将环境变量传递到脚本中的awscli命令中?
任何帮助都将不胜感激。
谢谢你!

bcs8qyzn

bcs8qyzn1#

变量在单引号内不展开。
如果在变量展开之前关闭单引号,然后立即打开单引号,应该会产生预期的效果。您可能需要也可能不需要将变量用双引号括起来,以便展开变量。

#!/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 "'"
        }]
      }
    }]
  }
  '
42fyovps

42fyovps2#

您还可以利用process substitution来保持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
)
wgx48brx

wgx48brx3#

这对我有用!

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

相关问题