|
provider "aws" { |
|
region = "us-east-2" |
|
} |
|
|
|
locals { |
|
ps_script_files = [ |
|
"winEc2-install-chocolatey.ps1", |
|
"winEc2-choco-install-git.ps1", |
|
"winEc2-choco-install-git-lfs.ps1", |
|
"winEc2-choco-install-docker-desktop.ps1", |
|
"winEc2-choco-install-dotnetfx.ps1", |
|
"winEc2-choco-install-nodejs.ps1", |
|
"winEc2-choco-install-temurin11.ps1", |
|
"winEc2-choco-install-vs2019-workload-vctools.ps1", |
|
"winEc2-install-rust.ps1", |
|
"winEc2-disable-pagefile-and-swapfile.ps1", |
|
] |
|
} |
|
|
|
resource "aws_ssm_document" "install_windows_bitbucket_runner_service" { |
|
name = "Run-winEc2-deploy-bitbucket-agent" |
|
document_type = "Command" |
|
target_type = "/AWS::EC2::Instance" |
|
content = jsonencode({ |
|
schemaVersion = "2.2" |
|
|
|
parameters = { |
|
BitBucketAccountUuid = { |
|
type = "String" |
|
description = "BitBucket Account UUID" |
|
default = "{YOUR_BITBUCKET_ACCOUNT_UUID}" # this should *include* the outer braces {} in the string |
|
} |
|
BitBucketRunnerUuid = { |
|
type = "String" |
|
description = "BitBucket Runner UUID" |
|
default = "<get this value from bitbucket UI>" # this should include the outer braces {} in the string |
|
} |
|
BitBucketOauthClientId = { |
|
type = "String" |
|
description = "BitBucket OAuth Client ID" |
|
default = "<get this value from bitbucket UI>" |
|
} |
|
BitBucketOauthSecret = { |
|
type = "String" |
|
description = "BitBucket OAuth Secret specific to this runner UUID" |
|
default = "<get this value from bitbucket UI>" |
|
} |
|
} |
|
mainSteps = [ |
|
{ |
|
action = "aws:runPowerShellScript" |
|
name = "RunScript" |
|
|
|
precondition = { |
|
StringEquals = [ |
|
"platformType", |
|
"Windows", |
|
] |
|
} |
|
|
|
inputs = { |
|
runCommand = [ |
|
file("powershell-scripts/winEc2-deploy-bitbucket-agent.ps1"), |
|
] |
|
} |
|
} |
|
] |
|
}) |
|
} |
|
|
|
resource "aws_ssm_document" "scripts" { |
|
for_each = { for file in local.ps_script_files : file => file } |
|
name = "Run-${replace(each.key, ".ps1", "")}" # e.g. Run-BitbucketRunner-01 |
|
document_type = "Command" |
|
target_type = "/AWS::EC2::Instance" |
|
content = jsonencode({ |
|
schemaVersion = "2.2" |
|
|
|
mainSteps = [ |
|
{ |
|
action = "aws:runPowerShellScript" |
|
name = "RunScript" |
|
|
|
precondition = { |
|
StringEquals = [ |
|
"platformType", |
|
"Windows", |
|
] |
|
} |
|
|
|
inputs = { |
|
runCommand = [ |
|
file("powershell-scripts/${each.value}"), |
|
] |
|
} |
|
} |
|
] |
|
}) |
|
} |
|
|
|
|
|
|
|
resource "aws_ssm_document" "bitbucket_runner_setup" { |
|
name = "Bitbucket-Windows-Runner-Setup" |
|
document_type = "Automation" |
|
document_format = "YAML" |
|
|
|
content = yamlencode({ |
|
schemaVersion = "0.3" |
|
description = "Runs PowerShell setup scripts for Bitbucket Windows runners." |
|
assumeRole = "{{ AutomationAssumeRole }}" |
|
parameters = { |
|
InstanceId = { |
|
type = "String" |
|
description = "Target instance ID" |
|
} |
|
AutomationAssumeRole = { |
|
type = "String" |
|
description = "Target instance ID" |
|
default = aws_iam_role.ssm_automation_role.arn |
|
} |
|
} |
|
mainSteps = [ |
|
for idx, script in local.ps_script_files : { |
|
name = "${replace(replace(script, ".ps1", ""), "-", "")}" |
|
action = "aws:runCommand" |
|
inputs = { |
|
DocumentName = "Run-${replace(script, ".ps1", "")}" |
|
InstanceIds = ["{{ InstanceId }}"] |
|
} |
|
} |
|
] |
|
}) |
|
|
|
depends_on = [aws_ssm_document.scripts] |
|
} |
|
|
|
resource "aws_iam_role" "ssm_automation_role" { |
|
name = "SSMAutomationRole" |
|
|
|
assume_role_policy = jsonencode({ |
|
Version = "2012-10-17" |
|
Statement = [{ |
|
Effect = "Allow" |
|
Principal = { |
|
Service = "ssm.amazonaws.com" |
|
} |
|
Action = "sts:AssumeRole" |
|
}] |
|
}) |
|
} |
|
|
|
resource "aws_iam_role_policy_attachment" "ssm_automation_policy" { |
|
role = aws_iam_role.ssm_automation_role.name |
|
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonSSMAutomationRole" |
|
} |
|
|
|
data "aws_instances" "bitbucket_runners" { |
|
filter { |
|
name = "tag:bitbucket-windows-runner" |
|
values = ["true"] |
|
} |
|
|
|
filter { |
|
name = "instance-state-name" |
|
values = ["running"] |
|
} |
|
} |