Skip to content

Instantly share code, notes, and snippets.

@jftuga
Created Jul 6, 2021
Embed
What would you like to do?
Terraform and ACM (AWS Certificate Manager)
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.48"
}
}
required_version = ">= 1.0.0"
}
variable "domain_name" {
description = "domain name without leading dot or without www"
type = string
default = "example.com"
}
variable "profile" {
description = "AWS user with ACM and Route53 permissions"
type = string
default = "default"
}
variable "region" {
description = "AWS Region for ACM"
type = string
default = "us-east-1"
}
provider "aws" {
profile = var.profile
region = var.region
}
resource "aws_acm_certificate" "myawsdomain" {
domain_name = var.domain_name
validation_method = "DNS"
subject_alternative_names = [format("*.%s", var.domain_name)]
lifecycle {
create_before_destroy = true
}
}
data "aws_route53_zone" "selected" {
name = format("%s.", var.domain_name)
private_zone = false
}
resource "aws_route53_record" "myawsdomain" {
for_each = {
for dvo in aws_acm_certificate.myawsdomain.domain_validation_options : dvo.domain_name => {
name = dvo.resource_record_name
record = dvo.resource_record_value
type = dvo.resource_record_type
}
}
allow_overwrite = true
name = each.value.name
records = [each.value.record]
ttl = 60
type = each.value.type
zone_id = data.aws_route53_zone.selected.zone_id
}
#!/bin/bash
# This creates an ACM certificate with the prerequistes that the domain is already hosted by AWS Route 53.
# Certificates are created for:
# MyNewDomain.com
# *.MyNewDomain.com
T=terraform
${T} init
${T} fmt
${T} validate
# to deploy:
${T} plan
${T} apply -var "region=us-east-1" -var "profile=ec2-admin" -var "domain_name=MyNewDomain.com"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment