top of page

Create an AWS EC2 instance with Ansible

To create an AWS EC2 instance with Ansible, you can use the ec2 module. Here's an example Ansible playbook that creates a t2.micro instance with CentOS 7:

AWS EC2 with Ansible


name: A description for the playbook.

hosts: localhost is used as we run this playbook locally.

gather_facts: Set to False to avoid gathering facts about the local machine.

ec2 module: This module is used to launch EC2 instances.

key_name: Replace with the name of your EC2 key pair.

region: Specify your AWS region (e.g., us-east-1).

instance_type: Set to t2.micro.

image: Specify the AMI ID for CentOS 7. You'll need to replace ami-XXXXXXXXXXXXXXXXX with the correct CentOS 7 AMI ID.

wait: Set to "yes" to wait for the instance to be in a running state.

count: Number of instances to launch (1 in this case).

group: Your security group name.

vpc_subnet_id: Specify the subnet ID in your VPC.

assign_public_ip: Set to "yes" to assign a public IP to the instance.

register: This stores the information about the newly launched instance in the ec2_instance variable.

add_host: This adds the newly launched instance to a host group named "launched" so you can target it in subsequent plays.



bottom of page