Archive for August, 2017

Amazon Linux useful stuff

August 24, 2017

The easiest way to get ec2 instance remediation via Auto Scaling is to implement health check API and put it behind Load Balancer. But still it can take 5-10 mins till ELB detects ec2 failure and tells ASG to tear it down.

EC2 System and Instance Status Checks monitor CPU, memory, os, file system, network and hardware of the instance (like loss of network sudo ifdown eth0). But they don’t give a sh*t about failure of custom software running on the instance – unless you tell them to.

This is how ec2 instance can explicitly tell Auto Scaling Group to replace it:

AWS_ACCESS_KEY_ID=AMYIDMYIDMYQ AWS_SECRET_ACCESS_KEY=0MYKEYMYKEYMYKEYMYKEYK aws autoscaling set-instance-health --instance-id "$(curl http://169.254.169.254/latest/meta-data/instance-id)" --health-status Unhealthy --region ap-southeast-2

It should have AWS credentials of a user with proper IAM permissions though.

Ansible playbook snippets

August 18, 2017

tasks/main.yml

Task: Get AMI Ids of AWS ec2 instances:

---
- name: get EC2 facts
  action: ec2_facts
  register: the_facts

– debug: var=the_facts.ansible_facts.ansible_ec2_ami_id

Task: Get python disto in use:

- name: Find the path to the python interpreter
  command: which python
  register: pythonpath

Playbook: Create an EC2 instance

---
- name: AWS connect
  hosts: localhost
  connection: local
  gather_facts: false
  tasks:
  - name: Launch new Instance
    become: false
    ec2:
      aws_access_key: AKIAJBSRYRJL4JIWSAGQ
      aws_secret_key: 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
      instance_tags: "Name=AnsibleTest"
      group_id: sg-c8af43ae
      instance_type: t2.micro
      image: ami-10918173
      wait: true
      region: ap-southeast-2
      keypair: ai-apphost
      vpc_subnet_id: subnet-f2011596
    register: ec2

Task: display IDs of running instances:

- ec2_remote_facts:
 region: ap-southeast-2
 register: thefacts

# - debug: var=item.id
 # with_items: "{{thefacts.instances}}"
 
 - debug: var=item.0
 with_together:
 - "{{ thefacts.instances|map(attribute='id')|list }}"

Inspired by : https://github.com/bonovoxly/ansible_snippets