Tag: terraform

  • Terraform for_each and output values

    Let’s say you have conditionally created a “thing” using for_each:

    resource "aws_instance" "this" {
        for_each = var.create_me == true ? toset(["yaaay"]) : toset([])
      # ...
    }
    

    Now you want to use the instance’s IP address, lets say in the module’s “output”, just to have an example (you can use it within the module as well, anywhere you want). I always have to look into existing code to remember, so here are the multiple possible ways I’ve used so far 🙂 :

    output "instance_ip_address_method_1" {
      type        = string
      value       = element(values(aws_instance.this), 0)["public_ip"]
    }
    
    output "instance_ip_address_method_2" {
      type        = string
      value       = element([for v in values(aws_instance.this) : v.public_ip], 0)
    }
    
    output "instance_ip_address_method_3" {
      description = "method 3"
      type        = string
      value       = aws_instance.this["yaaay"].public_ip
    }
    
  • Managing S3 bucket permissions with terraform

    This is what I did today, and I found it useful. And yes, sometimes I over-engineer a bit.

      # IN FILE: locals_constants.tf
    
      iam_s3_perms_list = ["s3:List*",]
      iam_s3_perms_read = ["s3:List*", "s3:Get*",]
      iam_s3_perms_write = ["s3:List*", "s3:Get*", "s3:PutObject",]
    
      s3_permissions_rw = distinct(concat(
        local.iam_s3_perms_list,
        local.iam_s3_perms_read,
        local.iam_s3_perms_write,
      ))
    
      s3_permissions_ro = distinct(concat(
        local.iam_s3_perms_list,
        local.iam_s3_perms_read,
      ))
    
    
    # IN FILE: locals.tf
    
    team_dev_bucket_configs = {
      "ReadWrite" = {
        actions = local.s3_permissions_rw
        buckets = [
          "yo-corp-yoman-dev",
          "yo-corp-yoman-test",
          "yo-corp-playground",
        ]
      }
      "ReadOnly" = {
        actions = local.s3_permissions_ro
        buckets = [
          "yo-corp-yoman-prod",
          "yo-corp-yoman-archive",
        ]
      }
    }
    
    
    # IN FILE: permissions.tf
    
    # so this policy is now assigned to a group with every dev team member.
    # easy. :)
    resource "aws_iam_policy" "s3_team_dev_bucket_permissions" {
      name        = "s3-team-dev-bucket-permissions"
      description = "Dev team S3 bucket permissions"
      policy      = data.aws_iam_policy_document.s3_team_dev_bucket_permissions_document.json
    }
    
    data "aws_iam_policy_document" "s3_team_dev_bucket_permissions_document" {
      dynamic "statement" {
        for_each = local.team_dev_bucket_configs
        content {
          sid     = "TeamDevBuckets${statement.key}"
          effect  = "Allow"
          actions = statement.value["actions"]
          resources = flatten(
            [
              for bucket in statement.value["buckets"] :
              [
                "arn:aws:s3:::${bucket}", "arn:aws:s3:::${bucket}/*"
              ]
            ]
          )
        }
      }
    }
    
  • AWS – auto-join Windows clients to a managed AD

    Notes:

    • This post is part of a series: Part 1
    • I still consider this post a “snippet”, cause it is just a bit of – hopefully – copy-pastable Terraform code, with a bit of explanation.)

    All right. in the previous post we created a VPC including an AD, and configured route53 to forward requests regarding “AD stuff” to it. But, an AD is quite useless without Windows clients. And Windows clients are annoying, cause you have to join them to the AD. Luckily, there’s an automation for that.

    AWS introduced the AWS Systems Manager (formerly SSM), which is a small software component pre-installed on most (all?) AWS AMIs and connects to an AWS-internal management instance to perform actions. Like “domain auto-join”.

    Approach

    There are two ways to do this:

    1. Configure each host to join the AD, or
    2. Tell AWS to perform the auto-join action on every host with a certain tag

    I’m following option (2) here, using the tag key/value combination { "adjoin": "true" }. Easy.

    WARNING: AWS SSM was key in at least one massive data leak. SO BE VERY CAREFUL in how you use the Systems Manager and if in doubt refer to further documentation. I think this approach is safe, because we do not use any credentials in the following code.

    Having said that, let’s go.

    The code

    resource "aws_ssm_document" "ad_join_domain" {
      name          = "ad-join-domain"
      document_type = "Command"
      content = jsonencode(
        {
          "schemaVersion" = "2.2"
          "description"   = "aws:domainJoin"
          "mainSteps" = [
            {
              "action" = "aws:domainJoin",
              "name"   = "domainJoin",
              "inputs" = {
                "directoryId"    = aws_directory_service_directory.ad.id,
                "directoryName"  = aws_directory_service_directory.ad.name,
                "dnsIpAddresses" = aws_directory_service_directory.ad.dns_ip_addresses
              }
            }
          ]
        }
      )
    }
    
    resource "aws_ssm_association" "windows_server" {
      name = aws_ssm_document.ad_join_domain.name
      targets {
        key    = "tag:adjoin"
        values = ["true"]
      }
    }
    
    resource "aws_iam_role" "ad_autojoin" {
      name = "ad-autojoin"
      assume_role_policy = jsonencode({
        "Version" = "2012-10-17",
        "Statement" = [
          {
            "Effect" = "Allow",
            "Principal" = {
              "Service" = "ec2.amazonaws.com"
            },
            "Action" = "sts:AssumeRole"
          }
        ]
      })
    }
    
    # required it seems
    resource "aws_iam_role_policy_attachment" "ssm-instance" {
      role       = aws_iam_role.ad_autojoin.id
      policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
    }
    
    # required it seems
    resource "aws_iam_role_policy_attachment" "ssm-ad" {
      role       = aws_iam_role.ad_autojoin.id
      policy_arn = "arn:aws:iam::aws:policy/AmazonSSMDirectoryServiceAccess"
    }
    
    resource "aws_iam_instance_profile" "ad_autojoin" {
      name = "ad-autojoin"
      role = aws_iam_role.ad_autojoin.name
    }
    

    The Windows instance

    Some notes:

    • NOTE: I am using a terraform module here, but in my code I am using a modified version of it. So it might be that this code needs some adjustments.
    • AWS does not yet provide Windows 10 images as AMIs. So we use Windows Server.

    Let’s go.

    data "aws_ami" "win2019server" {
      owners      = ["amazon"]
      most_recent = true
    
      filter {
        name   = "name"
        values = ["Windows_Server-2019-German-Full-Base*"]
      }
    
      filter {
        name   = "platform"
        values = ["windows"]
      }
    
      filter {
        name   = "architecture"
        values = ["x86_64"]
      }
    }
    
    module "ec2-instance" {
      source  = "terraform-aws-modules/ec2-instance/aws"
      version = "3.2.0"
    
      ami           = data.aws_ami.win2019server.id
      instance_type = "t3.large"
      cpu_credits   = "unlimited"
      subnet_id     = element(module.vpc.private_subnets, 0)
    
      # THIS IS WHAT WE NEED TO AUTO_JOIN!! :))
      iam_instance_profile = aws_iam_instance_profile.ad_autojoin.name
      tags                 = merge({ "adjoin" = "true" }, local.tags)
    
      # yes, weird syntax
      root_block_device = [{
        volume_size = 50
        tags = {
          Name = "my-root-block"
        }
      }]
    }
    

    References

  • AWS – create VPC and a managed AD with terraform

    Note:

    • This post is part of a series: Part 2

    AWS offers a managed AD service, in two flavors (Samba-based, and “original” Microsoft). This shows how you create …

    • a VPC
    • a managed Microsoft AD
    • an internal DNS zone
    • an internal DNS forwarder for the “AD domain” to the AD

    … with terraform.

    Let’s go.

    Part 1 – the VPC and the AD

    locals {
      name                  = "testproject"
      internal_dns_zone     = "testproject.local"
      ad_domain             = "testad.aws"
      cidr                  = "10.0.0.0/24"
      azs                   = ["eu-central-1b", "eu-central-1c"]
      database_subnets      = [cidrsubnet(local.cidr, 3, 0), cidrsubnet(local.cidr, 3, 1)]
      private_subnets       = [cidrsubnet(local.cidr, 3, 2)]
      public_subnets        = [cidrsubnet(local.cidr, 3, 3)]
      secret_winad_password = "uiuiuiui_super_secret"
      tags                  = { broughttoyouby = "flypenguin ;)" }
    }
    
    module "vpc" {
      source  = "terraform-aws-modules/vpc/aws"
      version = "3.7.0"
    
      azs                   = local.azs
      cidr                  = local.cidr
      create_igw            = true # for jump hosts, see also: https://is.gd/2Ry3H7
      database_subnets      = local.database_subnets
      enable_dns_hostnames  = true
      enable_nat_gateway    = true
      enable_vpn_gateway    = false # not yet
      name                  = local.name
      private_subnets       = local.private_subnets
      public_subnets        = local.public_subnets
      single_nat_gateway    = true
    }
    
    resource "aws_directory_service_directory" "ad" {
      name      = local.ad_domain
      password  = local.secret_winad_password
      edition   = "Standard"
      type      = "MicrosoftAD"
      tags      = local.tags
    
      vpc_settings {
        vpc_id     = module.vpc.vpc_id
        subnet_ids = module.vpc.database_subnets
      }
    }
    
    resource "aws_route53_zone" "vpc" {
      name = local.internal_dns_zone
    
      vpc {
        vpc_id = module.vpc.vpc_id
      }
    }
    

    Part 2 – the resolver

    Now this was pretty easy. Problem: Windows thinks it is responsible for performing autoritative (I think) DNS lookups for the AD “domain”. And the internal DNS resolver has absolutely no clue about any AD related stuff. Amazon itself offers two possibilities:

    1. using the managed AD as the primary DNS “servers”, forwarding requests to route53, or
    2. configure route53 to forward to the managed AD for requests regarding “testad.aws” (our AD “domain”)

    This describes option (1), because in my eyes the VPC should not stop functioning without the AD (which it does if you give the AD primary DNS responsibility).

    resource "aws_route53_resolver_endpoint" "ad_hcm_resolver" {
      name      = "ad-hcm-resolver"
      direction = "OUTBOUND"
      tags      = local.tags
    
      ip_address { subnet_id = module.vpc.database_subnets[0] }
      ip_address { subnet_id = module.vpc.database_subnets[1] }
    }
    
    resource "aws_route53_resolver_rule" "ad_fwd" {
      domain_name          = local.ad_domain
      name                 = "ad_hcm_forward"
      rule_type            = "FORWARD"
      resolver_endpoint_id = aws_route53_resolver_endpoint.ad_hcm_resolver.id
      tags                 = local.tags
    
      target_ip { ip = sort(aws_directory_service_directory.ad.dns_ip_addresses)[0] }
      target_ip { ip = sort(aws_directory_service_directory.ad.dns_ip_addresses)[1] }
    }
    
    # WE NEED THIS. without it does not work.
    resource "aws_route53_resolver_rule_association" "ad_fwd" {
      resolver_rule_id = aws_route53_resolver_rule.ad_fwd.id
      vpc_id           = module.vpc.vpc_id
    }
    

    WARNING: That stupid forwarder costs about 180$ / month (about 90¢ per IP address, and you must have two). It is REALLY expensive for what it does.

  • The state of things – technology

    It’s been a while. I am currently pretty burned out, and the work keeps getting more. This is bad. But let’s talk about some challenges right now. So this is an overview of our …

    Technical state

    We’re still using Rancher. Rancher is super cool, but has the annoying habit of completely crashing about once every two months, leading to a full cluster outage for anything between 1-3 hours. Usually about 2. I still love it, but we matured in our needs, and maybe Rancher needs time to catch up (cause our needs are sometimes a bit “special”). But the Rancher team is making great progress in the right directions, and I am fully competent that Rancher will take a place in the orchestration space. Still we’re thinking about moving to K8s, simply because so much is already there.

    We’re using Prometheus for monitoring now. Rocks. Period.

    We’re still using AWS. Many of our customers would prefer Azure Germany. If you didn’t know – Azure in Germany advertises a “Data Custodian” mode, or “Data Trustee” model, not sure how to translate this and too lazy to look it up. This means that in Germany the data centers are running the true Azure stuff, but they are actually fully operated by Deutsche Telekom.

    Advantage, you ask? Easy. When the DOJ sends one of those super secure letters to Microsoft for “give me your data”, they simply forward this to Deutsche Telekom. They will probably frame it on a wall somewhere, but I don’t think they will actually give out the data. Problem solved. (We all hope :))

    We are almost done with setting the whole cloud up using Terraform. It became a really mature project over the last year, and we are super happy with the progress it’s making. Also, with Azure in the works for us (some cusomers …) this is a cool way to just manage all with the same tooling. Infrastructure as code, eh.

    We try to migrate away from Teamcity to Jenkins. We didn’t succeed yet. Too little manpower.

    But the more interesting thing is in the next post, for me at least 😉