From 7d41bd6afdc920f064060595000380623af56da9 Mon Sep 17 00:00:00 2001 From: Rodrigo Fernandes Date: Sun, 18 Nov 2018 19:07:55 +0000 Subject: [PATCH] Initial terraform setup --- .circleci/config.yml | 2 +- .gitignore | 3 + terraform/main.tf | 150 +++++++++++++++++++++++++++++++++++++++++ terraform/outputs.tf | 7 ++ terraform/variables.tf | 19 ++++++ 5 files changed, 180 insertions(+), 1 deletion(-) create mode 100644 terraform/main.tf create mode 100644 terraform/outputs.tf create mode 100644 terraform/variables.tf diff --git a/.circleci/config.yml b/.circleci/config.yml index 286af13..56d463f 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -88,7 +88,7 @@ jobs: - run: name: Deploy working_directory: ~/diff2html/docs - command: aws s3 sync . s3://diff2html.rtfpessoa.xyz --region eu-west-1 + command: aws s3 sync . s3://diff2html.xyz --region eu-west-1 workflows: version: 2 diff --git a/.gitignore b/.gitignore index 5d961ec..bde6b2f 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,6 @@ coverage/ # Bower bower_components/ + +# Terraform +/terraform/.terraform diff --git a/terraform/main.tf b/terraform/main.tf new file mode 100644 index 0000000..bd533c6 --- /dev/null +++ b/terraform/main.tf @@ -0,0 +1,150 @@ +# Inspired by https://gist.github.com/danihodovic/a51eb0d9d4b29649c2d094f4251827dd + +provider "aws" { + profile = "${var.aws_profile}" + region = "${var.aws_region}" +} + +provider "aws" { + alias = "nvirginia" + profile = "${var.aws_profile}" + region = "us-east-1" +} + +terraform { + backend "s3" { + region = "us-east-1" + encrypt = true + bucket = "terraform-state-bucket.rtfpessoa.xyz" + dynamodb_table = "terraform-state-table" + key = "diff2html.xyz" + } +} + +resource "aws_acm_certificate" "cert" { + provider = "aws.nvirginia" + domain_name = "${var.domain}" + subject_alternative_names = ["*.${var.domain}"] + validation_method = "DNS" + + lifecycle { + create_before_destroy = true + } +} + +resource "aws_route53_record" "root_domain" { + zone_id = "${var.hosted_zone_id}" + name = "${var.domain}" + type = "A" + + alias { + name = "${aws_cloudfront_distribution.cdn.domain_name}" + zone_id = "${aws_cloudfront_distribution.cdn.hosted_zone_id}" + evaluate_target_health = false + } +} + +resource "aws_route53_record" "www_domain" { + zone_id = "${var.hosted_zone_id}" + name = "www.${var.domain}" + type = "A" + + alias { + name = "${aws_cloudfront_distribution.cdn.domain_name}" + zone_id = "${aws_cloudfront_distribution.cdn.hosted_zone_id}" + evaluate_target_health = false + } +} + +resource "aws_route53_record" "cert_validation" { + zone_id = "${var.hosted_zone_id}" + name = "${aws_acm_certificate.cert.domain_validation_options.0.resource_record_name}" + type = "${aws_acm_certificate.cert.domain_validation_options.0.resource_record_type}" + + records = ["${aws_acm_certificate.cert.domain_validation_options.0.resource_record_value}"] + ttl = 60 +} + +resource "aws_acm_certificate_validation" "cert" { + provider = "aws.nvirginia" + certificate_arn = "${aws_acm_certificate.cert.arn}" + validation_record_fqdns = ["${aws_route53_record.cert_validation.fqdn}"] +} + +resource "aws_cloudfront_origin_access_identity" "origin_access_identity" { + comment = "${var.domain} origin access identity" +} + +resource "aws_s3_bucket" "site" { + bucket = "${var.domain}" + acl = "private" + + policy = <