Create a WordPress server with Ansible on AWS – Step 1

The idea is to fully script the creation of a WordPress server including the database on CentOS 8 and Amazon Web Services. For this Ansible will be used. I will describe this in several steps/posts.

Step 1 Create the Linux server
Step 1 is to create the Linux server from a bastion host within AWS. For now we will use CentOS 7 because CentOS 8 is not availabe. Later it will be changed for CentOS 8.

Update Ansible to the latest version
yum -y update ansible

Install boto3 because that is the SDK for AWS.
yum -y install python-pip
pip install --upgrade pip
pip install boto3

$ ansible --version
ansible 2.8.2

Set the AWS credentials via environment variables:
export AWS_ACCESS_KEY_ID="XXXX"
export AWS_SECRET_ACCESS_KEY="XXXX"

Create the script in a file create-centos7-aws.yml

The CentOS AMI ID can be found for instance here: https://wiki.centos.org/Cloud/AWS


# create-centos7-aws.yml
#
- hosts: localhost
  connection: local
  gather_facts: yes
  tasks:
  - name: Create CentOS instance on AWS
    ec2_instance:
      image_id: ami-6e28b517
      instance_initiated_shutdown_behavior: stop
      instance_type: t2.micro
      key_name: xxxx-keypair-eu-ie
      name: myinstance
      region: eu-west-1
      tags:
        Environment: test
      volumes:
      - device_name: /dev/sda1
        ebs:
          volume_size: 8
      vpc_subnet_id: subnet-xxxx
      wait: yes
      wait_timeout: 600

Run the script

ansible-playbook create-centos7-aws.yml --become-user centos

This is all to create an instance on AWS. It takes some time for the script to be finished.