Infra/AWS

AWS Ansible 을 이용한 EC2 생성

sonny.kim 2018. 1. 25. 11:18

Build an EC2 using Ansible Step By Step

이 글은 Anabilities와 몇 가지 부가 기능을 사용하여 AWS 내에서 EC2 인스턴스를 생성 (스핀 업)하는 방법을 단계별로 설명합니다.

다음 명령을 실행하여 Ansible 및 AWS에 필요한 종속성을 설치합니다. 루트 사용자가 아닌 SUDO 액세스 권한이있는 일반 사용자로 실행합니다.

sudo pip install --upgrade pip

sudo pip install boto

sudo yum install ansible

AWS 계정에 로그인하여 "AWS_ACCESS_KEY_ID"및 "AWS_SECRET_ACCESS_KEY"를 받습니다.  “Identity and Access Management”로 이동하여 새 사용자를 만들거나 기존 사용자를 선택합니다.. "보안 자격 증명"으로 이동하여 "ACCESS_KEY"를 클릭합니다.

Access Key ID: NUHKOIJFOJF9GFJDO

Secret Access Key: LSDJKFODSJF9SDJF8UH3U3HFKW

AWS 보안키 설정:

export AWS_ACCESS_KEY_ID="NUHKOIJFOJF9GFJDO"

export AWS_SECRET_ACCESS_KEY="LSDJKFODSJF9SDJF8UH3U3HFKW"

~/hosts 파일 설정

[local]

localhost

 

[webserver]

이제 Anabilities가 실행 가능한 YML 파일을 빌드합니다. 다음은 공개 IP 주소와 공용 SSH 키를 사용하여 기본 EC2를 생성하는 샘플입니다. 다음 파일을 "~ / ec2-basic.yml"파일에 넣으십시오.

---

  - name: Provision an EC2 Instance

    hosts: local

    connection: local

    gather_facts: False

    tags: provisioning

    # Necessary Variables for creating/provisioning the EC2 Instance

    vars:

      instance_type: t2.micro

      security_group: ansible-webserver # Change the security group name here

      image: ami-719fb712 # This is an AMI i created myself

      keypair: agix-key # This is one of my keys that i already have in AWS

      region: ap-southeast-2 # Change the Region

      count: 1

 

    # Task that will be used to Launch/Create an EC2 Instance

    tasks:

 

      - name: Create a security group

        local_action:

          module: ec2_group

          name: "{{ security_group }}"

          description: Security Group for webserver Servers

          region: "{{ region }}"

          rules:

            - proto: tcp

              from_port: 22

              to_port: 22

              cidr_ip: 0.0.0.0/0

            - proto: tcp

              from_port: 80

              to_port: 80

              cidr_ip: 0.0.0.0/0

            - proto: tcp

              from_port: 443

              to_port: 443

              cidr_ip: 0.0.0.0/0

          rules_egress:

            - proto: all

              cidr_ip: 0.0.0.0/0

        register: basic_firewall

 

      - name: Launch the new EC2 Instance

        local_action: ec2

                      group={{ security_group }}

                      instance_type={{ instance_type}}

                      image={{ image }}

                      wait=true

                      region={{ region }}

                      keypair={{ keypair }}

                      count={{count}}

        register: ec2

 

      - name: Add the newly created EC2 instance(s) to the local host group (located inside the directory)

        local_action: lineinfile

                      dest="./hosts"

                      regexp={{ item.public_ip }}

                      insertafter="[webserver]" line={{ item.public_ip }}

        with_items: ec2.instances

 

 

      - name: Wait for SSH to come up

        local_action: wait_for

                      host={{ item.public_ip }}

                      port=22

                      state=started

        with_items: ec2.instances

 

      - name: Add tag to Instance(s)

        local_action: ec2_tag resource={{ item.id }} region={{ region }} state=present

        with_items: ec2.instances

        args:

          tags:

            Name: webserver

Provisioning 시작 (spin it up):

ansible-playbook -i ./hosts ec2-basic.yml

생성된 인스턴스에 로그인:

ssh -l centos 54.1.2.3 -i Downloads/agix-key.pem