deploy ec2 instance and join to aws managed Domain

432 views
Skip to first unread message

Tony Wong

unread,
Jun 29, 2020, 9:48:45 AM6/29/20
to terrafo...@googlegroups.com
I created a AWS managed domain. I used packer to and created custom AMi on aws.

how do i use terraform to created the instance and join it to the domain?


david...@mycit.ie

unread,
Jun 29, 2020, 9:53:28 AM6/29/20
to Terraform
Use the "User_Data" Within Terraform: 
This will host your PowerShell Script to Join your Machine to the Domain

Tony Wong

unread,
Jun 29, 2020, 12:29:33 PM6/29/20
to terrafo...@googlegroups.com
=I am not getting dns settings set with this. I was able to rename the computer and password

variable "admin_password" {}
variable "ami" {}
variable "aws_instance_type" {}
variable "key_name" {}
variable "user_data_path" {}
provider "aws" {
region = "us-west-1"
}

data "template_file" "init" {
template = <<EOF
<powershell>
$admin = [ADSI]("WinNT://./administrator, user")
$admin.SetPassword("${var.admin_password}")
Set-DNSClientServerAddress –interfaceIndex 12 –ServerAddresses (“172.31.2.31”,”172.31.22.29”)
$NewComputerName = "win2016-test"
Rename-Computer -NewName $NewComputerName
Start-Sleep -Seconds 5
Restart-Computer -Force
</powershell>
EOF
vars = {
admin_password = "${var.admin_password}"
}
}

resource "aws_instance" "ec2" {
ami = var.ami
instance_type = var.aws_instance_type
key_name = var.key_name
user_data = data.template_file.init.rendered

connection {
host = coalesce(self.public_ip, self.private_ip)
type = "winrm"
user = "Administrator"
password = var.admin_password
}

tags = {
Srv = "windows"
}
}

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/52a55097-94cd-4106-92d4-cd914db78d1bn%40googlegroups.com.

Tony Wong

unread,
Jun 30, 2020, 8:38:19 AM6/30/20
to terrafo...@googlegroups.com
any idea how to join my ec2 instance to my aws managed domain? I looks at using SSM document but cant figure out how.

I already got the AWS AD domain setup and giving 2 DNS IPs. 

I verified I was able to join the domain manually on the ec2 instance

david...@mycit.ie

unread,
Jun 30, 2020, 10:48:37 AM6/30/20
to Terraform

Tony Wong

unread,
Jul 8, 2020, 2:54:08 PM7/8/20
to terrafo...@googlegroups.com
I got ec2 and aws AD domain provisioned. Trying to use the SSM document to join the domain but unable to do so. no errors from output

the EC2 instance still is in workgroup

ssm.tf is here



resource "aws_ssm_document" "join_domain_doc" {
name = "join_domain_doc"
document_type = "Command"

content = <<DOC
{
"schemaVersion": "1.0",
"description": "Join an instance to a domain",
"runtimeConfig": {
"aws:domainJoin": {
"properties": {
"directoryId": "${aws_directory_service_directory.AD.id}",
"directoryName": "${var.dir_domain_name}",
"directoryOU": "${var.dir_computer_ou}",
"dnsIpAddresses": ["172.31.2.172", "172.31.31.146"]
}
}
}
}
DOC

depends_on = ["aws_directory_service_directory.AD"]
}

resource "aws_ssm_association" "join_domain_doc" {
name = "join_domain_doc"
instance_id = "${aws_instance.ec2.id}"
depends_on = ["aws_ssm_document.join_domain_doc", "aws_instance.ec2"]
}
any idea?


Adam

unread,
Jul 8, 2020, 3:00:02 PM7/8/20
to terrafo...@googlegroups.com
Is the SSM document deployed to AWS? If so, have you run it manually and checked the output (you can run them and send output to Cloudwatch). Once you get that, you can post the error here and someone may help. Without any errors, it's practically impossible to help you.

david...@mycit.ie

unread,
Jul 8, 2020, 3:46:38 PM7/8/20
to Terraform
Isn't the Topic "Error creating SSM document: InvalidDocumentContent" that you also Started the Same issue? 

Tony Wong

unread,
Jul 8, 2020, 4:04:14 PM7/8/20
to terrafo...@googlegroups.com
That has been resolved 

My instance is not joining domain



Tony Wong

unread,
Jul 8, 2020, 4:08:40 PM7/8/20
to terrafo...@googlegroups.com
How do I send output to cloudwatch?

david...@mycit.ie

unread,
Jul 8, 2020, 4:15:39 PM7/8/20
to Terraform

Adam

unread,
Jul 9, 2020, 4:45:11 AM7/9/20
to terrafo...@googlegroups.com
If you scroll down, you'll see a "cloudwatch output" check box.

Tony Wong

unread,
Jul 9, 2020, 10:12:55 AM7/9/20
to terrafo...@googlegroups.com
do you see a problem with this? There is no error now but machine is not joining. 
I changed it to schemaversion 2

resource "aws_ssm_document" "join_domain" {
name = "join_domain"
   document_type = "Command"

content = <<DOC
{
   "schemaVersion": "2.0",
"description": "Run a PowerShell script to securely domain-join a Windows instance",
"mainSteps": [{
"action": "aws:runPowerShellScript",
"name": "runPowerShellWithSecureString",
"inputs": {
"runCommand": [
"$ipdns = (Get-SSMParameterValue -Name /domain/dns_ip).Parameters[0].Value\n",
"$domain = (Get-SSMParameterValue -Name /domain/name).Parameters[0].Value\n",
"$ouPath = (Get-SSMParameterValue -Name /domain/ou_path).Parameters[0].Value\n",
"$username = (Get-SSMParameterValue -Name /domain/username).Parameters[0].Value\n",
"$domain_username = \"$domain\\$username\"\n",
"echo $domain_username\n",
"$password = (Get-SSMParameterValue -Name /domain/password -WithDecryption $True).Parameters[0].Value | ConvertTo-SecureString -asPlainText -Force\n",
"$credential = New-Object System.Management.Automation.PSCredential($domain_username,$password)\n",
"Set-DnsClientServerAddress \"Ethernet\" -ServerAddresses $ipdns\n",
"Add-Computer -DomainName $domain -OUPath \"$ouPath\" -Credential $credential\n",
"Restart-Computer -Force"
]
}
}]
}
DOC

depends_on = ["aws_directory_service_directory.AD"]
}

resource "aws_ssm_association" "join_domain" {
name = "join_domain"
targets {
key = "InstanceIds"
values = ["${aws_instance.ec2.id}"]
}
}

Fernando 🐼

unread,
Jul 9, 2020, 10:16:11 AM7/9/20
to terrafo...@googlegroups.com
you can probably get better and quicker support by either contacting aws support or terraform discuss group

Adam

unread,
Jul 9, 2020, 1:48:12 PM7/9/20
to terrafo...@googlegroups.com
Could you post the output of the SSM document running?


Tony Wong

unread,
Jul 9, 2020, 2:09:07 PM7/9/20
to terrafo...@googlegroups.com
do you mean my output from terraform?

Adam

unread,
Jul 9, 2020, 2:56:58 PM7/9/20
to terrafo...@googlegroups.com
No. The output from the ssm document running via cloudwatch. 

Reply all
Reply to author
Forward
0 new messages