In this article, we will take a look at
- AWS OpsWorks Stack
- Add new or existing server to OpsWorks Stack
- Add custom cookbooks
This article assumes that user already has:
- An AWS account
- IAM user with required permission
For example: User can use IAM to grant permissions for opsworks:CreateStack and opsworks:CloneStack, which are used to create and clone stacks respectively.
AWS OpsWorks Stack
AWS OpsWorks is a configuration management service that uses Chef (an automation platform that treats server configurations as a code). OpsWorks Stack uses to manage an application on AWS servers and on-premises servers. AWS OpsWorks stack scales the instances based on time or load on it.
Steps to create an AWS OpsWork stack
1. Store Chef custom cookbook repository to SCM
With Chef, user develops cookbooks on the workstation, stores them to SCM and deploys it to Chef client. In order to use AWS OpsWorks, user must put cookbooks in one of the SCM tool. When a user installs or updates a cookbook, AWS OpsWorks Stacks installs the entire repository in a local cache on each of the stack’s instances. When an instance needs, for example, to run one or more recipes, it retrieves the code from the local cache.
2. Create an AWS OpsWorks Stack
Let’s launch OpsWorks Stack from the OpsWorks Console.
- Click on ‘Go to OpsWorks Stack’ to get started as shown in fig 2.1
Fig. 2.1 OpsWorks Console
- Select Chef version then Enter stack name, Region, VPC and Choose one of user’s SSH key pairs.
- Finally, click on ‘Add stack’. ()
Fig. 2.2 Creating Stack
3. Configure stack to use custom cookbooks
- In order to use custom cookbooks in a stack, user must configure the stack to enable custom cookbooks (if it is not already configured).
- User needs to provide the cookbook repository information such as repository URL and password/ssh key.
Fig. 3.1 Adding Custom Cookbooks
4. Add layers into stack
User has to add layers into the stack, by clicking on ‘Add layer’ button as a shown in fig 4.1
Fig. 4.1 Adding Layer
5. Register Chef Clients with AWS OpsWorks
User can add either new or existing instance to a layer:
- “New” – OpsWorks creates a new instance, configured as per user’s specifications, and makes it a member of the layer.
- “Existing” – User can add an existing instance to any compatible layer
New – To add a new instance to a layer:
To add new instances into a layer
- Click on ‘Add instance’
- On an instance page, choose ‘New’ button
- Configure Hostname and Size of an instance.
- If user wants to configure more than just the Hostname, Size and Subnet or Availability Zone – “choose Advanced >>” option/tab to see more options. The following fig. 5.1 shows the complete set of options:
Fig. 5.1 Adding New Instance
Existing – To add an existing instance to a layer:
To add existing EC2 instances or on-premises instances into AWS OpsWorks layer, user has to register that instances with OpsWorks Stack.
- Click on ‘Add instance’
- On the Instances page, choose ‘EC2 instances and own servers’
- Click on ‘Register an instance
- Then follow the instructions provided after clicking “Register an instance” link
6. Execute cookbooks/recipes to layer
User can execute recipes in two ways:
- Automatically – By assigning recipes to the appropriate layer’s lifecycle event.
- Manually – By running the Execute Recipes stack command or by using the agent CLI
Each layer has a set of five lifecycle events. When an event occurs on a instance, AWS OpsWorks Stacks automatically runs the appropriate set of recipes.
Type of LifeCycle Events:
Setup – This event occurs after instance has finished booting.
Configure – This event occurs on the stack’s instances when one of the following occur:
- An instance enters or leaves the online state or
- User associates an Elastic IP address with an instance or disassociates it from an instance or
- User attaches an Elastic Load Balancer to a layer or detaches it from a layer.
Deploy – This event occurs when user runs a Deploy command, typically to deploy an application across set of application server instances.
Undeploy – This event occurs when user deletes an app or runs an Undeploy command to remove an app from a set of application server instances.
Shutdown – This event occurs when user shutdown instance from AWS Opsworks Stack but before the associated Amazon EC2 instance is actually terminated.
1. Automatically Running Recipes
When a lifecycle event occurs on an instance, AWS OpsWorks Stacks run the appropriate set of recipes for the associated layer. To apply custom cookbooks, assign recipe to a layer’s lifecycle event. After an event occurs, AWS OpsWorks Stacks run the specified custom recipes.
To assign custom recipes to layer events:
- On the Layers page for the appropriate layer, click ‘Recipes’ and then click ‘Edit’
- On the Recipes tab, enter custom recipe in the appropriate event field
- Click the “+” button to add the multiple recipes
Fig. 6.1 Adding Recipes to Layer
2. Manually Running Recipes
User can manually run recipes at any time on Stack instances. To run a custom recipe manually, it must exist in any of the user’s custom cookbooks, but it does not have to be assigned to a particular lifecycle event.
To manually run recipes on Stack instances:
- On the Stack page, click ‘Run command’. From Command tab, select ‘Execute Recipes’.
- Enter the recipes to be run in the “Recipes to execute” box by using the standard format – “cookbookname::recipename”. Use commas to separate multiple recipes; they will run in the order that user lists them.
- Under Instances, select the instances on which AWS OpsWorks Stacks should run the recipes.
Fig. 6.2 Manually Running Recipes
AWS OpsWorks helps user to configure and run applications using Chef. AWS OpsWorks Stacks is useful for System administrators and ops-minded developers, who are looking for a powerful end-to-end configuration management solution. Using OpsWorks Stacks user can automatically scale servers based on time or load on an application.