├── .gitignore ├── terraform.tfvars ├── files ├── terraform-aws-profile.png ├── userdata.sh ├── conf.wp-config.php └── cust.sql ├── providers.tf ├── output.tf ├── vars.tf ├── security.tf ├── README.md ├── ec2-database.tf └── vpc.tf /.gitignore: -------------------------------------------------------------------------------- 1 | *state* 2 | .terraform/ 3 | *.state* 4 | .* -------------------------------------------------------------------------------- /terraform.tfvars: -------------------------------------------------------------------------------- 1 | username = "pavan" 2 | password = "pavan12345" 3 | dbname = "mydb" 4 | -------------------------------------------------------------------------------- /files/terraform-aws-profile.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aleti-pavan/terraform-aws-wordpress/HEAD/files/terraform-aws-profile.png -------------------------------------------------------------------------------- /providers.tf: -------------------------------------------------------------------------------- 1 | provider "aws" { 2 | region = var.aws_reg 3 | # version = "2.12.0" 4 | profile = "terraform" 5 | } 6 | 7 | provider "template" { 8 | version = "~> 2.1.2" 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /output.tf: -------------------------------------------------------------------------------- 1 | output "ami_id" { 2 | value = data.aws_ami.ubuntu.id 3 | } 4 | 5 | output "Login" { 6 | value = "ssh -i ${aws_key_pair.keypair1.key_name} ubuntu@${aws_instance.ec2.public_ip}" 7 | } 8 | 9 | output "azs" { 10 | value = data.aws_availability_zones.azs.*.names 11 | } 12 | 13 | output "db_access_from_ec2" { 14 | value = "mysql -h ${aws_db_instance.mysql.address} -P ${aws_db_instance.mysql.port} -u ${var.username} -p${var.password}" 15 | } 16 | 17 | output "access" { 18 | value = "http://${aws_instance.ec2.public_ip}/index.php" 19 | } 20 | -------------------------------------------------------------------------------- /vars.tf: -------------------------------------------------------------------------------- 1 | variable aws_reg { 2 | description = "This is aws region" 3 | default = "eu-west-2" 4 | type = string 5 | } 6 | 7 | variable stack { 8 | description = "this is name for tags" 9 | default = "terraform" 10 | } 11 | 12 | variable username { 13 | description = "DB username" 14 | } 15 | 16 | variable password { 17 | description = "DB password" 18 | } 19 | 20 | variable dbname { 21 | description = "db name" 22 | } 23 | 24 | variable ssh_key { 25 | default = "~/.ssh/id_rsa.pub" 26 | description = "Default pub key" 27 | } 28 | 29 | variable ssh_priv_key { 30 | default = "~/.ssh/id_rsa" 31 | description = "Default private key" 32 | } 33 | -------------------------------------------------------------------------------- /files/userdata.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | sudo echo "127.0.0.1 `hostname`" >> /etc/hosts 3 | sudo apt-get update -y 4 | sudo apt-get install mysql-client -y 5 | sudo apt-get install apache2 apache2-utils -y 6 | sudo apt-get install php5 -y 7 | sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt php5-curl php5-gd php5-xmlrp -y 8 | sudo apt-get install php5-mysqlnd-ms -y 9 | sudo service apache2 restart 10 | sudo wget -c http://wordpress.org/wordpress-5.1.1.tar.gz 11 | sudo tar -xzvf wordpress-5.1.1.tar.gz 12 | sleep 20 13 | sudo mkdir -p /var/www/html/ 14 | sudo rsync -av wordpress/* /var/www/html/ 15 | sudo chown -R www-data:www-data /var/www/html/ 16 | sudo chmod -R 755 /var/www/html/ 17 | sudo cp /var/www/html/wp-config-sample.php /var/www/html/wp-config.php 18 | sudo service apache2 restart 19 | sleep 20 20 | -------------------------------------------------------------------------------- /security.tf: -------------------------------------------------------------------------------- 1 | resource aws_security_group "mysql" { 2 | name = "${var.stack}-DBSG" 3 | description = "managed by terrafrom for db servers" 4 | vpc_id = aws_vpc.vpc.id 5 | 6 | tags = { 7 | Name = "${var.stack}-DBSG" 8 | } 9 | 10 | ingress { 11 | protocol = "tcp" 12 | from_port = 3306 13 | to_port = 3306 14 | security_groups = ["${aws_security_group.web.id}"] 15 | } 16 | 17 | egress { 18 | protocol = -1 19 | from_port = 0 20 | to_port = 0 21 | cidr_blocks = ["0.0.0.0/0"] 22 | } 23 | } 24 | 25 | resource aws_security_group "web" { 26 | name = "${var.stack}-webSG" 27 | description = "This is for ${var.stack}s web servers security group" 28 | vpc_id = "${aws_vpc.vpc.id}" 29 | 30 | tags = { 31 | Name = "${var.stack}-webSG" 32 | } 33 | 34 | ingress { 35 | protocol = "tcp" 36 | from_port = 22 37 | to_port = 22 38 | cidr_blocks = ["0.0.0.0/0"] 39 | } 40 | 41 | ingress { 42 | protocol = "icmp" 43 | from_port = -1 44 | to_port = -1 45 | cidr_blocks = [aws_vpc.vpc.cidr_block] 46 | } 47 | 48 | ingress { 49 | protocol = "tcp" 50 | from_port = 80 51 | to_port = 80 52 | cidr_blocks = ["0.0.0.0/0"] 53 | } 54 | 55 | egress { 56 | protocol = -1 57 | from_port = 0 58 | to_port = 0 59 | cidr_blocks = ["0.0.0.0/0"] 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | What's this: 2 | ========= 3 | 4 | This is to set up wordpress using AWS infrastructure. We are using terraform to provision infrastructure. Code uses and creates following aws services. 5 | 6 | 1. VPC and it's components 7 | 2. Subnets, Route Tables, Internet Gateway, Nat Gateway. 8 | 3. EC2 instance 9 | 4. EIP for NAT Gateway 10 | 5. RDS mysql instance. 11 | 6. Security Groups to access both EC2 and MYSQL 12 | 13 | Note: 14 | ----- 15 | You may get charged by aws for using services 16 | 17 | 18 | ### Pre-requisite: 19 | 20 | 1. You need to have Ssh keys generated and should be put into `~/.ssh/` , if your machine is windows then feel free to use diff path and update the same in the `ssh_key` variable in the `vars.tf` 21 | 22 | 2. create an IAM user and create security credentials(AccessKey, SecretKey) and update in the `~/.aws/credentials` file like below 23 | ![terraform-aws-profile](files/terraform-aws-profile.png) 24 | 25 | __Note__ : if you have default profile, just erase the `profile` attribute in `provider.tf` 26 | 27 | 28 | Usage: 29 | ======= 30 | 31 | provisioning: 32 | ------------- 33 | 34 | 1. git clone https://github.com/aleti-pavan/terraform-aws-wordpress.git 35 | 2. cd terraform-aws-wordpress 36 | 2. terraform init 37 | 3. terraform plan 38 | 4. terraform apply -auto-approve 39 | 40 | Destroying the Infra: 41 | --------------------- 42 | 1. cd terraform-aws-wordpress (Be in the repo directory) 43 | 2. terraform destroy -auto-approve 44 | 45 | 46 | 47 | Change: (latest detail from top) 48 | ------ 49 | 50 | Code has been changed on 11th April, 2020. Code is now compatible with below versions. 51 | 52 | Versions: 53 | -------- 54 | Terraform v0.12.24 55 | + provider.aws v2.12.0 56 | + provider.template v2.1.2 57 | 58 | 59 | 60 | __(Old)__ Code slightly changed on 31st May, 2019. 61 | I have added providers.tf with versions required for each provider -------------------------------------------------------------------------------- /files/conf.wp-config.php: -------------------------------------------------------------------------------- 1 |