1

I would like to create a Bash script that will start and stop specific resources in AWS. The problem I have is I would like to create a resource the requires the a specific resource ID that was created by the previous command. The goal is to be able to run a single script to start the resources instead of editing.

For example creating a NAT gateway then appending the ID of the NAT-gateway created to a route table command:

$ aws ec2 create-nat-gateway

Sample output:

"NatGatewayId": "nat-1111111"

$ aws ec2 replace-route --route-table-id rtb-000000000 \
  --destination-cidr-block 0.0.0.0/0 --nat-gateway-id "***nat-id-output***"
1
  • Have a look at ansible Commented Sep 6, 2018 at 0:45

1 Answer 1

1
O=$(aws ec2 create-nat-gateway | perl -pe 's/.*: //g');
while true ; do
    C=$(aws ect describe-nat-gateways --nat-gateway-ids "$O" | grep -c available || true);
    if [ $C -gt 0 ] ; then
        break;
    fi
    sleep 3;
done
aws ec2 replace-route --route-table-id rtb-000000000 --destination-cidr-block 0.0.0.0/0 --nat-gateway-id "$O";
5
  • I very much appreciate yor help but i am getting a ./test-script1.sh: line 3: $: command not found ./test-script1.sh: line 4: $: command not found
    – Matt
    Commented Sep 6, 2018 at 1:32
  • This is how my final command looks with your syntax and the proper id's inserted
    – Matt
    Commented Sep 6, 2018 at 1:33
  • @Matt slm edited my answer to confuse you, please review. Commented Sep 6, 2018 at 1:36
  • Thank you for your help but as I feared this wont work due to the fact that aws takes time to create the nat gateway thus editing the route table via a script is not immediately available so it fails. Will have to likely use a declarative program such as chef or ansible.
    – Matt
    Commented Sep 6, 2018 at 1:45
  • @Matt You can just wait in a loop, almost any language can accomplish almost any task. Commented Sep 6, 2018 at 2:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .