How to deploy an opsworks application by cloudformation?

380 views Asked by At

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.

2

There are 2 answers

0
wjordan On BEST ANSWER

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.)
2
Manish Joshi On

I believe this can be done within the recipes. So in your recipes you'll have a function to validate the app name and if it exists then proceed with the deployment.

For example your deploy recipe would look something like this:

 if validator(node[:app][:name]) == true
   do whatever
  end  

and this validator function can reside in your chef library:

def validator(app_name)
  app = search("aws_opsworks_app", "name:#{app_name}").first
  if app[:deploy] == true
     Chef::Log.warn("PROCEEDING: Deploy initiated for #{app[:name]}")
  end
end