-
Notifications
You must be signed in to change notification settings - Fork 0
fix: SSH PEM 키 의존성 제거 → SSM RunCommand 방식으로 전환 #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,7 +55,37 @@ resource "aws_instance" "api_server" { | |
| } | ||
| } | ||
|
|
||
| # 설정 및 컨테이너 실행 | ||
| locals { | ||
| nginx_script_b64 = base64encode(templatefile("${path.module}/scripts/nginx_setup.sh.tftpl", { | ||
| domain_name = var.domain_name | ||
| email = var.cert_email | ||
| conf_file_name = var.nginx_conf_name | ||
| })) | ||
|
|
||
| alloy_config = templatefile("${path.module}/../../config/side-infra/config.alloy.tftpl", { | ||
| loki_ip = data.aws_instance.monitoring_server.private_ip | ||
| }) | ||
|
|
||
| side_infra_script_b64 = base64encode(templatefile("${path.module}/scripts/side_infra_setup.sh.tftpl", { | ||
| work_dir = var.work_dir | ||
| alloy_env_name = var.alloy_env_name | ||
| alloy_config_content = local.alloy_config | ||
| redis_version = var.redis_version | ||
| redis_exporter_version = var.redis_exporter_version | ||
| alloy_version = var.alloy_version | ||
| })) | ||
|
|
||
| nginx_ssm_params = jsonencode({ | ||
| commands = ["cloud-init status --wait > /dev/null", "echo ${local.nginx_script_b64} | base64 -d | sudo bash"] | ||
| executionTimeout = ["3600"] | ||
| }) | ||
|
|
||
| side_infra_ssm_params = jsonencode({ | ||
| commands = ["cloud-init status --wait > /dev/null", "echo ${local.side_infra_script_b64} | base64 -d | sudo bash"] | ||
| executionTimeout = ["3600"] | ||
| }) | ||
| } | ||
|
|
||
| # [리소스 1] Nginx 설정 변경 감지 및 실행 | ||
| resource "null_resource" "update_nginx" { | ||
| depends_on = [aws_instance.api_server] | ||
|
|
@@ -68,30 +98,39 @@ resource "null_resource" "update_nginx" { | |
| })) | ||
| } | ||
|
|
||
| connection { | ||
| type = "ssh" | ||
| user = "ubuntu" | ||
| host = aws_instance.api_server.public_ip | ||
| private_key = file(var.ssh_key_path) | ||
| } | ||
|
|
||
| provisioner "file" { | ||
| content = templatefile("${path.module}/scripts/nginx_setup.sh.tftpl", { | ||
| domain_name = var.domain_name | ||
| email = var.cert_email | ||
| conf_file_name = var.nginx_conf_name | ||
| }) | ||
| destination = "/tmp/update_nginx.sh" | ||
| } | ||
|
|
||
| provisioner "remote-exec" { | ||
| inline = [ | ||
| "cloud-init status --wait > /dev/null", # Docker 설치 대기 | ||
| "chmod +x /tmp/update_nginx.sh", | ||
| "echo 'Running Updated Nginx Script...'", | ||
| "sudo /tmp/update_nginx.sh", | ||
| "rm /tmp/update_nginx.sh" | ||
| ] | ||
| provisioner "local-exec" { | ||
| interpreter = ["bash", "-c"] | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이미 저희 운영 방칙 상 액션 러너(우분투)로 진행하도록 합의를 했었기에 괜찮다고 생각합니다. 이미 로컬 apply를 막게 정했기에 문제가 없을 것 같습니다! 해당 부분은 협업 워크플로우 wiki 문서에 문서화되어 있으니 확인부탁드립니다~ |
||
| command = <<-EOT | ||
| set -euo pipefail | ||
| INSTANCE_ID='${aws_instance.api_server.id}' | ||
| COMMAND_ID=$(aws ssm send-command \ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기서 Terraform 실행 주체가 직접 EC2 IAM Instance Profile의 현재 bootstrap 코드에서는
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 권한은 이미 확인했습니다. PR 설명에 오해가 있었네요;; 해당 설명 수정하였습니다! |
||
| --instance-ids "$INSTANCE_ID" \ | ||
| --document-name "AWS-RunShellScript" \ | ||
| --parameters '${local.nginx_ssm_params}' \ | ||
| --output text \ | ||
| --query "Command.CommandId") | ||
| ATTEMPTS=0 | ||
| while [ "$ATTEMPTS" -lt 360 ]; do | ||
| STATUS=$(aws ssm get-command-invocation \ | ||
| --command-id "$COMMAND_ID" \ | ||
| --instance-id "$INSTANCE_ID" \ | ||
| --query "Status" --output text 2>/dev/null || echo "Pending") | ||
| case "$STATUS" in | ||
| Success) exit 0 ;; | ||
| Failed|Cancelled|TimedOut|Undeliverable) | ||
| echo "SSM command $STATUS" >&2 | ||
| aws ssm get-command-invocation \ | ||
| --command-id "$COMMAND_ID" \ | ||
| --instance-id "$INSTANCE_ID" \ | ||
| --query "StandardErrorContent" --output text >&2 | ||
| exit 1 ;; | ||
| esac | ||
| ATTEMPTS=$((ATTEMPTS + 1)) | ||
| sleep 10 | ||
| done | ||
| echo "SSM command timed out after 3600s" >&2 | ||
| exit 1 | ||
| EOT | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -112,34 +151,38 @@ resource "null_resource" "update_side_infra" { | |
| })) | ||
| } | ||
|
|
||
| connection { | ||
| type = "ssh" | ||
| user = "ubuntu" | ||
| host = aws_instance.api_server.public_ip | ||
| private_key = file(var.ssh_key_path) | ||
| } | ||
|
|
||
| provisioner "file" { | ||
| content = templatefile("${path.module}/scripts/side_infra_setup.sh.tftpl", { | ||
| work_dir = var.work_dir | ||
| alloy_env_name = var.alloy_env_name | ||
| alloy_config_content = templatefile("${path.module}/../../config/side-infra/config.alloy.tftpl", { | ||
| loki_ip = data.aws_instance.monitoring_server.private_ip | ||
| }) | ||
| redis_version = var.redis_version | ||
| redis_exporter_version = var.redis_exporter_version | ||
| alloy_version = var.alloy_version | ||
| }) | ||
| destination = "/tmp/update_side_infra.sh" | ||
| } | ||
|
|
||
| provisioner "remote-exec" { | ||
| inline = [ | ||
| "cloud-init status --wait > /dev/null", # Docker 설치 대기 | ||
| "chmod +x /tmp/update_side_infra.sh", | ||
| "echo 'Running Updated Side Infra Script...'", | ||
| "sudo /tmp/update_side_infra.sh", | ||
| "rm /tmp/update_side_infra.sh" | ||
| ] | ||
| provisioner "local-exec" { | ||
| interpreter = ["bash", "-c"] | ||
| command = <<-EOT | ||
| set -euo pipefail | ||
| INSTANCE_ID='${aws_instance.api_server.id}' | ||
| COMMAND_ID=$(aws ssm send-command \ | ||
| --instance-ids "$INSTANCE_ID" \ | ||
| --document-name "AWS-RunShellScript" \ | ||
| --parameters '${local.side_infra_ssm_params}' \ | ||
| --output text \ | ||
| --query "Command.CommandId") | ||
| ATTEMPTS=0 | ||
| while [ "$ATTEMPTS" -lt 360 ]; do | ||
| STATUS=$(aws ssm get-command-invocation \ | ||
| --command-id "$COMMAND_ID" \ | ||
| --instance-id "$INSTANCE_ID" \ | ||
| --query "Status" --output text 2>/dev/null || echo "Pending") | ||
| case "$STATUS" in | ||
| Success) exit 0 ;; | ||
| Failed|Cancelled|TimedOut|Undeliverable) | ||
| echo "SSM command $STATUS" >&2 | ||
| aws ssm get-command-invocation \ | ||
| --command-id "$COMMAND_ID" \ | ||
| --instance-id "$INSTANCE_ID" \ | ||
| --query "StandardErrorContent" --output text >&2 | ||
| exit 1 ;; | ||
| esac | ||
| ATTEMPTS=$((ATTEMPTS + 1)) | ||
| sleep 10 | ||
| done | ||
| echo "SSM command timed out after 3600s" >&2 | ||
| exit 1 | ||
| EOT | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.