2

So I'm writing a load of infrastructure automation stuff atm, mainly revolving around Cloudformation, and I have a wrapper/framework tool that I'm using to manage the CF deploys (Sceptre). I've run up against a task that is not supported by CF, but is necessary for task completion and I'm tired of adding in manual intervention steps halfway through tasks. So what I'd like is a way to trigger commands when a specific command is run. For example:

sceptre create-stack example mystack

And I have an aws cli command to trigger when someone runs this.

aws module command --option=lol

Thanks.

EDIT: If someone has a linux native way of doing it, please provide it, I'd like to understand the multiple ways I could have completed this task.

2
  • 1
    Make a script that contains all these commands. Run that instead. Or extend the capability of Sceptre. It's Python, so you can just change it.
    – HackSlash
    Commented Apr 18, 2018 at 21:47
  • @HackSlash I get what you mean but that's doing what I don't want to do. I want other users to be able to use the framework without having to do context switches for different tasks, always feels like the fastest way to errors. Commented Apr 18, 2018 at 21:55

2 Answers 2

1

So I've found a way to do it actually, but it's not much of a superuser answer, so if mods want to delete, move or whatever this answer/question, feel free.

Using an AWS Lambda function in a custom resource

With Lambda functions and custom resources, you can run custom code in response to stack events (create, update, and delete). The following custom resource invokes a Lambda function and sends it the StackName property as input. The function uses this property to get outputs from the appropriate stack.

JSON

"MyCustomResource" : {
  "Type" : "Custom::TestLambdaCrossStackRef",
  "Properties" : {
    "ServiceToken": { "Fn::Join": [ "", [ "arn:aws:lambda:", { "Ref": "AWS::Region" }, ":", { "Ref": "AWS::AccountId" }, ":function:", {"Ref" : "LambdaFunctionName"} ] ] },
    "StackName": {
      "Ref": "NetworkStackName"
    }
  }
}

YAML

MyCustomResource: 
  Type: "Custom::TestLambdaCrossStackRef"
  Properties: 
    ServiceToken:
      !Sub |
        arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${LambdaFunctionName}
    StackName: 
      Ref: "NetworkStackName"
0

And I'm ashamed to say I found another way to do it, why didn't I just RTFM? So this is actually a way native to Sceptre.

Hooks allows the ability for custom commands to be run when Sceptre actions occur.

A hook is executed at a particular hook point when Sceptre is run.

If required, users can create their own hooks, as described in the section Custom Hooks. Hook points

before_create or after_create - run hook before or after stack creation.

before_update or after_update - run hook before or after stack update.

before_delete or after_delete - run hook before or after stack deletion.

Syntax:

Hooks are specified in a stack’s config file, using the following syntax:

hooks:
    hook_point:
        - !command_type command 1
        - !command_type command 2

You must log in to answer this question.

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