# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: MIT-0 resource "aws_iam_role" "sfn_preallocate_instance_role" { name_prefix = "${var.project}-${var.environment}-preallocate-role" assume_role_policy = jsonencode({ Version = "2012-10-17" Statement = [ { Action = "sts:AssumeRole" Effect = "Allow" Principal = { Service = "states.amazonaws.com" } }, ] }) inline_policy { name = "${var.project}-${var.environment}-ec2" policy = jsonencode({ Version = "2012-10-17" Statement = [ { Action = [ "ec2:CreateTags", "ec2:RunInstances", "ec2:DescribeInstanceStatus", "ec2:TerminateInstances" ] Effect = "Allow" Resource = ["*"] }, ] }) } inline_policy { name = "${var.project}-${var.environment}-iam" policy = jsonencode({ Version = "2012-10-17" Statement = [ { Action = [ "iam:PassRole" ] Effect = "Allow" Resource = [aws_iam_role.vdi_instance_role.arn] }, ] }) } inline_policy { name = "${var.project}-${var.environment}-ssm" policy = jsonencode({ Version = "2012-10-17" Statement = [ { Action = [ "ssm:StartAutomationExecution", "ssm:DescribeInstanceInformation", "ssm:GetAutomationExecution", "ssm:SendCommand", "ssm:ListCommands", "ssm:ListCommandInvocations", ] Effect = "Allow" Resource = ["*"] }, ] }) } inline_policy { name = "${var.project}-${var.environment}-dynamodb" policy = jsonencode({ Version = "2012-10-17" Statement = [ { Action = [ "dynamodb:PutItem", "dynamodb:UpdateItem", "dynamodb:DeleteItem" ] Effect = "Allow" Resource = aws_dynamodb_table.application_table.arn }, ] }) } inline_policy { name = "${var.project}-${var.environment}-kms" policy = jsonencode({ Version = "2012-10-17" Statement = [ { Action = [ "kms:CreateGrant", "kms:ReEncrypt*", "kms:GenerateDataKey*", "kms:Encrypt", "kms:Describe*", "kms:Decrypt" ] Effect = "Allow" Resource = var.kms_key_arn }, ] }) } inline_policy { name = "${var.project}-${var.environment}-x-ray" policy = jsonencode({ Version = "2012-10-17" Statement = [ { Action = [ "xray:PutTraceSegments", "xray:PutTelemetryRecords", "xray:GetSamplingRules", "xray:GetSamplingTargets" ] Effect = "Allow" Resource = "*" }, ] }) } inline_policy { name = "${var.project}-${var.environment}-logs" policy = jsonencode({ Version = "2012-10-17" Statement = [ { Action = [ "logs:CreateLogDelivery", "logs:GetLogDelivery", "logs:UpdateLogDelivery", "logs:DeleteLogDelivery", "logs:ListLogDeliveries", "logs:PutResourcePolicy", "logs:PutLogEvents", "logs:PutDestination", "logs:DescribeResourcePolicies", "logs:DescribeLogGroups", "logs:DescribeDestinations" ] Effect = "Allow" Resource = "*" }, ] }) } } resource "aws_cloudwatch_log_group" "sfn_preallocate_instance_logs" { name = "${var.project}-${var.environment}-sfn-preallocate-instance-logs" retention_in_days = 14 kms_key_id = var.kms_key_arn } resource "aws_sfn_state_machine" "preallocate_instance" { name = "${var.project}-${var.environment}-preallocate-instance" role_arn = aws_iam_role.sfn_preallocate_instance_role.arn logging_configuration { level = "ALL" include_execution_data = true log_destination = "${aws_cloudwatch_log_group.sfn_preallocate_instance_logs.arn}:*" } tracing_configuration { enabled = true } definition = <