Lab Guide: Building an Optimization Workflow


1. Lab Briefing

This section provides an overview of the lab challenge and instructions for getting started.

1.1. Challenge Summary

In this challenge, you will create a workflow. Public cloud environments are often used by many different teams, and by using automation, you can create workflows to help enforce organizational policies.

Example of a completed workflow

1.2. What is Infrastructure Optimization?

While a lot of cloud automation focuses on provisioning and de-provisioning, we can also use automation to help tame our public cloud environments and keep them under control. This is often called Day-2 operations or infrastructure optimization.

The cycle of infrastructure optimization

This can include tasks like identifying non-compliant resources, scheduling instances to be turned off outside of business hours, or finding and removing orphaned resources.

Example of identifying a non-compliant EC2 instance
Example of scheduling an instance to be stopped

1.3. Getting Started

That is the end of your challenge briefing! Please click the green Start button in the bottom right corner of this window if the lab has not already started.

Start Button


2. Lab Guide: Hands-On Tasks

Estimated time to complete: 20 minutes

In this third challenge, you will create a workflow that uses your existing job template to find specific instances and then performs an action on them.

2.1. Task 1: Log into Ansible Automation Platform

First, you will log in to the automation controller to begin the lab exercises.

  1. Navigate to the Automation Controller UI.

    Click on the Automation Controller tab at the top of your lab window.

  2. Log in with the provided credentials.

    Username

    admin

    Password

    ansible12-3!

After logging in, you will land on the main dashboard.

2.2. Task 2: Create the "Stop Instances" Job Template

You will start by creating a job template whose purpose is to stop EC2 instances.

  1. Navigate to the Templates page.

    In the left navigation menu, go to Automation ExecutionTemplates.

  2. Initiate the creation of a new job template.

    Click the Create template button, then select Create job template.

    Create a new job template

  3. Enter the job template details.

    Fill out the form with the following information:

    Parameter

    Value

    Name

    Stop instances

    Job Type

    Run

    Inventory

    Demo Inventory

    Project

    AWS Demos Project

    Execution Environment

    AWS Execution Environment

    Playbook

    playbooks/stop_aws_ec2_instances.yml

    Credentials

    AWS_Credential

    To find the AWS_Credential, you may need to filter the Credential Type to Amazon Web Services.
  4. Save the job template.

    Scroll to the bottom and click the blue Save button.

  5. Understand the playbook.

    This playbook contains a single task that uses the amazon.aws.ec2_instance module to stop instances passed to it via the identified_instances variable.

    - name: stop every un-tagged running instance in a region.
      amazon.aws.ec2_instance:
        region: "{{ ec2_region }}"
        state: stopped
        instance_ids: "{{ identified_instances }}"
      when: identified_instances | length > 0
The Ansible Playbooks for this lab are sourced from the following project: ansible-cloud/aws_demos.

2.3. Task 3: Build the AWS Optimization Workflow

Now, you will combine the template from the previous lab (Retrieve INSTANCES by tag) with the new Stop instances template into a single workflow.

  1. Navigate to the Templates page and initiate workflow creation.

    Go to Automation ExecutionTemplates, click the Create template button, and select Create workflow job template.

    Create a new workflow job template

  2. Enter the workflow details.

    Parameter

    Value

    Name

    AWS Workflow

    Organization

    Default

    Click Save. The Workflow Visualizer will open.

  3. Add the first node (Retrieve Instances).

    Click the Start button to add the first step. Configure it as follows:

    1. Node Type: Job Template

    2. Job Template: Select Retrieve INSTANCES by tag. Click Next.

    3. Survey: In the Provide an EC2 filter field, enter "tag:Name": "rhel1".

    4. Click Next, then Save to add the node.

  4. Add the second node (Stop Instances).

    Hover over the Retrieve INSTANCES by tag node, click the + icon, and select Add node. Configure it as follows:

    1. Node Type: Job Template

    2. Run: Select On Success.

    3. Job Template: Select Stop instances.

    4. Click Next, then Save.

  5. Save the workflow.

    In the top right corner of the Visualizer, click Save. Your completed workflow should look like this:

    Completed workflow diagram

2.4. Task 4: Launch the Workflow

Now you are ready to run the complete workflow.

  1. Navigate to the Templates page.

    Go to Automation ExecutionTemplates.

  2. Launch the workflow.

    Find the AWS Workflow in the list and click the Launch icon (🚀).

    Launch Job Icon

  3. Monitor the visualizer.

    The workflow visualizer will show the real-time progress. The workflow will:

  4. Run the Retrieve instances by tag node to identify the rhel1 instance.

  5. Upon success, run the Stop instances node, which will turn off the instance identified in the first step.

    You can click on any node in the visualizer to view the Output tab for that specific job.

2.5. Task 5: Verify the Instance is Stopped

Finally, you will confirm the result in the AWS Management Console.

  1. Navigate to the AWS Console.

    Click on the AWS console tab in your lab environment and log in with the provided credentials.

    Be careful to avoid extra spaces when copying and pasting the credentials.
  2. Go to the EC2 service.

    In the top search bar, search for and select EC2.

    AWS console search for EC2

  3. Check the instance status.

    Go to the Instances page. Make sure you are in the N. Virginia / us-east-1 region. You should see one instance in the Running state and the rhel1 instance in the Stopped state.

    Instance status in AWS console


3. Why Cloud Optimization is Important

You have successfully completed this challenge. This simple lab demonstrates a powerful concept for managing cloud costs and resources. Automation workflows can help you:

  • Turn off unused resources: Automatically shut down instances that are no longer needed.

  • Right-size cloud resources: Identify over-provisioned instances and adjust them.

  • Recover orphaned resources: Find and remove resources left behind by failed processes.

Imagine scheduling a workflow to run nightly, searching for any development instances left running for more than a few hours. This gives cloud teams peace of mind that their infrastructure is not incurring unnecessary charges.

4. Next Steps

Press the Check button in your lab environment to complete the challenge.

5. Troubleshooting

If you have encountered an issue or have noticed something not quite right, please link:https://github.com/ansible/instruqt/issues/new?title=Issue+with+Ansible+Hybrid+Cloud+Automation+-+Infrastructure+optimization&assignees=hichammourad[open, please