Automatically create an RFC in AMS using Python - AWS Prescriptive Guidance

Automatically create an RFC in AMS using Python

Created by Gnanasekaran Kailasam (AWS)

Summary

AWS Managed Services (AMS) helps you to operate your cloud-based infrastructure more efficiently and securely by providing ongoing management of your Amazon Web Services (AWS) infrastructure. To make a change to your managed environment, you need to create and submit a new request for change (RFC) that includes a change type (CT) ID for a particular operation or action.

However, manually creating an RFC can take around five minutes and teams in your organization might need to submit multiple RFCs every day. This pattern helps you to automate the RFC creation process, reduce the creation time for each RFC, and eliminate manual errors.   

This pattern describes how to use Python code to automatically create the Stop EC2 instance RFC that stops Amazon Elastic Compute Cloud (Amazon EC2) instances in your AMS account. You can then apply this pattern’s approach and the Python automation to other RFC types. 

Prerequisites and limitations

Prerequisites

  • An AMS Advanced account. For more information about this, see AMS operations plans in the AWS Managed Services documentation.

  • At least one existing EC2 instance in your AMS account.

  • An understanding of how to create and submit RFCs in AMS.

  • Familiarity with Python.

Limitations

  • You can only use RFCs for changes in your AMS account. Your AWS account uses different processes for similar changes.

Architecture

Technology stack  

  • AMS

  • AWS Command Line Interface (AWS CLI)

  • AWS SDK for Python (Boto3)

  • Python and its required packages (JSON and Boto3)

 

Automation and scale

This pattern provides sample code to automate the Stop EC2 instance RFC, but you can use this pattern’s sample code and approach for other RFCs.

Tools

  • AWS Managed Services – AMS helps you to operate your AWS infrastructure more efficiently and securely.

  • AWS CLI – AWS Command Line Interface (AWS CLI) is a unified tool to manage your AWS services. In AMS, the change management API provides operations to create and manage RFCs.

  • AWS SDK for Python (Boto3) – SDK for Python makes it easy to integrate your Python application, library, or script with AWS services. 

Code

The AMS Stop EC2 Instance.zip file (attached) contains the Python code for creating a Stop EC2 instance RFC. You can also configure this code to submit a single RFC for multiple EC2 instances.

Epics

TaskDescriptionSkills required
Install and validate Python.
  1. Open a terminal window and run the brew install python3 command.

  2. Validate that Python is correctly installed by running the python --version command.

  3. Validate that pip is correctly installed by running the pip --version command.

AWS systems administrator
Install AWS CLI.

Run the pip install awscli --upgrade –user command to install AWS CLI

AWS systems administrator
Install Boto3.

Run the pip install boto3 command to install Boto3.

AWS systems administrator
Install JSON.

Run the pip install json command to install JSON.

AWS systems administrator
Set up AMS CLI.

Sign in to the AWS Management Console, open the AMS console, and then choose Documentation. Download the .zip file that contains the AMS CLI, unzip it, and then install it on your local machine.

After you install AMS CLI, run the aws amscm help command. The output provides information about the AMS change management process.

AWS systems administrator
TaskDescriptionSkills required
Install and validate Python.
  1. Open the Python releases for Windows page, download the latest version, and then install Python.

  2. Validate that Python is correctly installed by running the python --version command.

  3. Validate that pip is correctly installed by running the pip --version command.

AWS systems administrator
Install AWS CLI.

Run the pip install awscli --upgrade –user command to install AWS CLI.

AWS systems administrator
Install Boto3.

Run the pip install boto3 command to install Boto3.

AWS systems administrator
Install JSON.

Run the pip install json command to install JSON.

AWS systems administrator
Set up AMS CLI.

Sign in to the AWS Management Console, open the AMS console, and then choose Documentation. Download the .zip file that contains the AMS CLI, unzip it, and then install it on your local machine.

After you install AMS CLI, run the aws amscm help command. The output provides information about the AMS change management process

AWS systems administrator
TaskDescriptionSkills required
Extract the CT ID, version, and execution parameters for the RFC.

Each RFC has a different CT ID, version, and execution parameters. You can extract this information by using one of the following options:

  1. Follow the instructions from the Finding a request for change (RFC) with the CLI section in RFC use examples from the AWS Managed Services documentation.  

  2. Open an existing RFC of a similar type or create new RFC as a test through the AMS console. Use the RFC’s CT ID and execution parameters. For more information about this, see Finding an RFC with the console in the AWS Managed Services documentation.

Note

To adapt this pattern’s Python automation for other RFCs, replace the CT type and parameter values in the ams_stop_ec2_instance Python code file from the AMS Stop EC2 Instance.zip file (attached) with those that you extracted.

AWS systems administrator
TaskDescriptionSkills required
Run the Python automation.
  1. Download the AMS Stop EC2 Instance.zip file (attached) to your local machine and extract the file.

  2. Update input_instances with your EC2 instance information.

  3. Open a terminal and navigate to the path for your extracted code

  4. Run the pythonams_stop_ec2_instance.py  command.

AWS systems administrator

Related resources

Attachments

To access additional content that is associated with this document, unzip the following file: attachment.zip