DEV Community

Cover image for Deploy a WordPress Site With Terraform & Ansible
Gurlal Sidhu
Gurlal Sidhu

Posted on

Deploy a WordPress Site With Terraform & Ansible

Quick Summary

  • Provision resources with Terraform
  • Configure EC2 instance with Ansible
  • Install CodeDeploy to deploy a website to an instance
  • Bash scripts to make changes afterward


  • Create SSH key

Architecture DiagramDiagram

Provision the resources with Terraform

Code is more than 150 lines long, so here's module link

Resources created with this module:

  • Service role for CodeDeploy

    CodeDeploy needs permissions to make changes to other resources to deploy the site. AWS managed policy and inline policy to get objects from S3 have been attached to the role.

  • Security group for EC2 Instance

    Allow HTTP traffic to the instance, so the website can be accessed from a browser.

  • Instance profile for EC2

    Permissions for the EC2 instance to make changes on other resources. The instance profile will use the same role that we created for CodeDeploy above.

  • EC2 Instance

    t2.micro(free-tier), add tags and security group provisioned above. SSH key to access your instance.

  • S3 bucket

    The S3 bucket where CodeDeploy pulls data from.

  • *CodeDeploy app and Deploy group

    Code Deployment group to deploy the site with CodeDeploy. CodeDeploy makes a selection of an EC2 instance based on tags.

  • Configure CodeDeploy group

    Settings for CodeDeploy deployment group.

Upon completion, Terraform will generate outputs (S3 id, CodeDeploy app, and CodeDeploy group name) that will get used in the Ansible playbook and bash script. Output file

Configure EC2 instance with Ansible.

Ansible folder on GitHub. Pasting the code here would take up a lot of space.

What's in the Ansible folder?

  • Inventory file

    Used aws plugin to do dynamic inventory.

  • Ansible config file

    Configure location for ssh key for EC2 instance and inventory file.

  • Scripts

    Dependencies to make WP site work on EC2 instance.

  • Playbook

    Download the site package, create a temp directory, unarchive the content, and copy all the dependencies

EC2 instance is ready!

Deploy CodeDeploy agent on EC2 using tags

aws ssm create-association --name AWS-ConfigureAWSPackage \
--targets Key=tag:Name,Values=codeDeploy --parameters action=Install,name=AWSCodeDeployAgent
Enter fullscreen mode Exit fullscreen mode

App deployment on the EC2 instance.

Be sure to update S3 bucket name and group-name based on Terraform outputs


aws deploy create-deployment --application-name WordPress_App --s3-location bucket=codedeploy-gsingh,bundleType=zip, \
--deployment-group-name WordPress_app_codedeploy_group --deployment-config-name CodeDeployDefault.OneAtATime
Enter fullscreen mode Exit fullscreen mode

Access the site using EC2's Public IPv4 DNS. If can't access the site, try to restart the httpd service on the instance.

Perform the steps below to complete the website setup

Choose Let's go!.

Database Name: test

User Name: root

Password: Leave blank.

Database Host: localhost

Table Prefix: wp_

Choose to Submit to set up the database.

Install WordPress.
Enter fullscreen mode Exit fullscreen mode

All done!

Try to make some modifications to the site configs, deploy with CodeDeploy, and see if CodeDeploy deploys those changes.

If you experience any issues, feel free to open up an issue on the GitHub repository or comment below. Thank you for reading :)

To see more articles like follow me here or Twitter

Discussion (0)