Search code examples
deploymentaws-cloudformationaws-opsworks

How to deploy an opsworks application by cloudformation?


In a cloudformation template, I create an opsworks stack, a layer, an instance and an application. This template sets up and configures the instance by a chef cookbook of recipes and scripts. How can I deploy the application automatically from the template without clicking manually on deploy inside the stack ? After the deploy the defined Deloy recipes from the cookbook are being executed:

"MyLayer": {
    "Type": "AWS::OpsWorks::Layer",
    "DependsOn" : "OpsWorksServiceRole",
    "Properties": {
     "AutoAssignElasticIps" : false,
     "AutoAssignPublicIps" : true,
     "CustomRecipes" : {
       "Setup"     : ["cassandra::setup","awscli::setup","settings::setup"],
       "Deploy": ["imports::deploy"]
    },
     "CustomSecurityGroupIds" : { "Ref" : "SecurityGroupIds" },
     "EnableAutoHealing" : true,
     "InstallUpdatesOnBoot": false,
     "LifecycleEventConfiguration": {
       "ShutdownEventConfiguration": {
       "DelayUntilElbConnectionsDrained": false,
       "ExecutionTimeout": 120 }
     },
     "Name": "script-node",
     "Shortname" : "node",
     "StackId": { "Ref": "MyStack" },
     "Type": "custom",
     "UseEbsOptimizedInstances": true,
     "VolumeConfigurations": [ {
       "Iops": 10000,
       "MountPoint": "/dev/sda1",
       "NumberOfDisks": 1,
       "Size": 20,
       "VolumeType": "gp2"
     }]
  }
}

An application looks like this: enter image description here

Any idea ? Thank you.


Solution

  • The CreateDeployment API call generates a one-off event that executes the Deploy actions within your OpsWorks stack. I don't think any official CloudFormation resource maps to this directly, but here are some ideas on how to call it within the context of a CloudFormation template:

    • Write a Custom Resource that calls CreateDeployment (e.g., via the AWS SDK for Node.js) when created.
    • Add an AWS::CodePipeline::Pipeline resource to your template that's configured to deploy your OpsWorks app as part of a Deploy Stage. See Using AWS CodePipeline with AWS OpsWorks Stacks for documentation on this integration. (Though it's an extra service + layer of complexity, I think CodePipeline is a better layer of abstraction for modeling deployment actions in your application stack anyway.)