Shashikant shah

Sunday 25 October 2020

ansible create ec2 instance and remove part-10

 How to create ec2 instance from ansible server.

  • Create IAM user .


  • Give to full access Permission.

  • Download key.


# vim  ~/.boto

[Credentials]

AWS_ACCESS_KEY_ID=AKIA3V6PJVJF3DUHGPIV

AWS_SECRET_ACCESS_EY=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


# vim  aws_provisioning.yml

---

- name: create a new Demo ec2 instance

  hosts: localhost

  connection: local

  gather_facts: False

  vars:

    instance_type: t2.micro

    security_group: default

    image: ami-032930428bf1abbff

    keypair: shashi

    region: us-east-1

 

  tasks:

    - name: create an ec2 instance

      ec2:

         key_name: "{{ keypair }}"

         group: "{{ security_group }}"  # security group name

         instance_type: "{{ instance_type}}"

         image: "{{ image }}"

         wait: true

         region: "{{ region }}"

         count: 1  # default

         count_tag:

            Name: Demo

         instance_tags:

            Name: Demo

         vpc_subnet_id: subnet-fb2bf1da

         assign_public_ip: yes

 

 How to remove instance from aws.

# vim aws_delete.yml

---

- hosts: localhost

  connection: local

  tasks:

    - name: Terminate instances that were previously launched

      ec2:

        state: 'absent'

        instance_ids: i-0256423e26c55a621

        region: us-east-1

 

No comments:

Post a Comment