Tomking Chen
Tomking Chen

Reputation: 363

Using sed with command-substitution text in the replacement part is not working

I have a CFN with cfn-init to deploy a Apache web server with specified virtual hosts. In the template, I use a AWS::CloudFormation::Init configset to replace local IPs with the Instance's private IP.

  config:
    packages:...
    files:...
    services:...
    commands:
      replacePrivateIP:
        cwd: "/etc/httpd/conf"
        command: !Sub |
          sed -i '[email protected]@$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4)@g' httpd.conf

The sed command works fine outside the CFN template. But in the CFN-init process, it simply replace "127.0.0.1" with the whole $(curl -s http://...) string.

How can I feed the instance private IP correctly into the httpd.conf file through cfn-init?

Upvotes: 1

Views: 295

Answers (1)

Inian
Inian

Reputation: 85683

The command-substitution syntax $(..) does not work when wrapped in single quotes which is as expected in bash or most other shells as they preserve the literal value present inside. For your substitution to happen, put it inside double-quotes as

sed -i '[email protected]@'"$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4)"'@g' httpd.conf

Compare the outputs of echo '$(date)' and echo "$(date)" for a simple example of your case.

Upvotes: 1

Related Questions