새소식

IaC/Packer

[Packer]AWS Session Manager를 통한 EC2 인스턴스 연결: github action에서 packer 실행하기

  • -

Packer를 사용하여 Amazon EC2 이미지를 빌드하는 과정에서, 내부 네트워크만 허용하는 환경에서 AWS의 Session Manager를 활용하는 방법을 소개합니다.

이 방법은 GitHub Actions를 사용하여 Packer 빌드를 수행할 때 특히 유용합니다.

Session Manager 연결 설정

Packer의 Amazon EBS 빌더를 사용할 때, ssh_interface를 "session_manager"로 설정하면 AWS Systems Manager를 통한 SSH 터널을 생성합니다. 이를 위해서는 인스턴스 프로필에 Systems Manager 권한이 필요합니다.

# file: example.pkr.hcl

# In order to get these variables to read from the environment,
# set the environment variables to have the same name as the declared
# variables, with the prefix PKR_VAR_.
# You could also hardcode them into the file, but we do not recommend that.

data "amazon-ami" "example" {
  filters = {
    virtualization-type = "hvm"
    name                = "ubuntu/images/*ubuntu-xenial-16.04-amd64-server-*"
    root-device-type    = "ebs"
  }
  owners      = ["099720109477"]
  most_recent = true
  region      = "us-east-1"
}

source "amazon-ebs" "ssm-example" {
  ami_name             = "packer_AWS {{timestamp}}"
  instance_type        = "t2.micro"
  region               = "us-east-1"
  source_ami           = data.amazon-ami.example.id

# provisioning connection parameters
  ssh_username         = "ubuntu"
  ssh_interface        = "session_manager"
  communicator         = "ssh"
  iam_instance_profile = "myinstanceprofile"
}

build {
  sources = ["source.amazon-ebs.ssm-example"]

  provisioner "shell" {
    inline = ["echo Connected via SSM at '${build.User}@${build.Host}:${build.Port}'"]
  }
}

Session Manager Connections

  • ssh_interface: The ssh interface must be set to "session_manager". When using this option the builder will create an SSM tunnel to the configured ssh_port (defaults to 22) on the remote host.
  • iam_instance_profile: A valid instance profile granting Systems Manager permissions to manage the remote instance is required in order for the aws ssm-agent to start and stop session connections. See below for more details on IAM instance profile for Systems Manager.

GitHub Actions를 위한 IAM 역할 설정: IAM instance profile for Systems Manager

GitHub Actions에서 Packer를 사용하여 AMI를 빌드할 때 필요한 IAM 권한을 설정합니다. 주요 권한으로는 iam:PassRole, iam:GetInstanceProfile, iam:CreateServiceLinkedRole 및 Systems Manager 관련 액션이 포함됩니다.

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": [
        "ec2:*",
        "iam:GetInstanceProfile",
        "iam:PassRole",
        "iam:CreateServiceLinkedRole",
        "ssm:StartSession",
        "ssm:TerminateSession"
      ],
      "Effect": "Allow",
      "Resource": "*"
    }
  ]
}

EC2 인스턴스 프로필 설정

AMI에 할당되는 EC2 인스턴스 프로필에는 AmazonSSMManagedInstanceCore 정책이 연결되어 있어야 합니다. 이 정책은 Systems Manager를 통한 인스턴스 관리에 필요한 기본 권한을 제공합니다.

정리

이러한 설정을 통해, 내부 네트워크만 허용하는 환경에서도 GitHub Actions를 사용하여 Packer를 통한 AMI 빌드를 원활하게 진행할 수 있습니다. AWS Session Manager는 SSH 접근이 제한된 환경에서도 안전하고 편리한 방법을 제공합니다.

'IaC > Packer' 카테고리의 다른 글

[Packer]AWS AMI Build with Ansible provisioner  (0) 2023.12.10
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.