aws-cloudformation / aws-cloudformation-resource-providers-awsutilities-commandrunner

Apache License 2.0
81 stars 21 forks source link

New functionality: add Teardown parameter #36

Open maslick opened 2 years ago

maslick commented 2 years ago

Right now we can do anything in the bash command (Command parameter) - we can fetch values from e.g. RDS, we can use AWS CLI to get some information, initialise databases, make external calls, etc. But we can also create new resources.

For instance Cloudformation does not support Storage Gateway (yet). So I am using CommandRunner to register Storage Gateway programmatically via AWS CLI, e.g.:

  StorageGatewayActivator:
    Type: AWSUtility::CloudFormation::CommandRunner
    Properties:
      SubnetId: !Ref SubnetId
      Role: !Ref ActivatorInstanceProfile
      LogGroup: !Ref ActivatorLogGroup
      Command: !Sub |
        curl -s "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
        unzip awscliv2.zip
        sudo ./aws/install
        alias aws="/usr/local/bin/aws"
        key=`curl "http://${StorageGateway.PrivateIp}/?gatewayType=FILE_S3&activationRegion=${AWS::Region}&no_redirect"`
        gw=`aws --region ${AWS::Region} storagegateway activate-gateway \
           --activation-key $key \
           --gateway-type FILE_S3 \
           --gateway-name ${AWS::StackName} \
           --gateway-timezone GMT+1:00 \
           --gateway-region ${AWS::Region} | jq -r .GatewayARN`
        sleep 30
        diskid=`aws --region ${AWS::Region} storagegateway list-local-disks --gateway-arn $gw | jq -r .Disks[0].DiskId`
        aws --region ${AWS::Region} storagegateway add-cache \
          --gateway-arn $gw \
          --disk-ids $diskid | jq -r .GatewayARN > /command-output.txt
        aws --region ${AWS::Region} storagegateway update-gateway-information \
          --gateway-arn $gw \
          --cloud-watch-log-group-arn ${StorageGWLogGroup.Arn} \
          --gateway-name ${AWS::StackName}
        aws --region ${AWS::Region} storagegateway create-nfs-file-share \
          --client-token token-$RANDOM \
          --gateway-arn $gw \
          --role ${StorageGatewayRole.Arn} \
          --location-arn ${BucketArn} \
          --client-list ${NfsClientIp} \
          --squash NoSquash \
          --vpc-endpoint-dns-name ${S3VpcEndpointDns} \
          --file-share-name ${AWS::StackName} \
          --bucket-region ${AWS::Region}
    DependsOn: StorageGatewayEC2

Now suppose I want to remove the stack. The only thing Cloudformation will remove is the Output value from Parameter Store. But it won't deactivate Storage gateway, nor will it detach the nfs file share.

So what we can do, is along with the Command parameter we can introduce another parameter e.g. TeardownCommand where we can add some teardown logic (bash script) and start a new CommandRunner stack (inside DeleteHandler.java).

shantgup commented 2 years ago

I will keep this as a FR.