Skip to content

Instantly share code, notes, and snippets.

View 100daysofdevops's full-sized avatar
🎯
Focusing

100daysofdevops

🎯
Focusing
View GitHub Profile
resource "aws_instance" "test_instance" {
count = "${var.instance_count}"
ami = "${data.aws_ami.centos.id}"
instance_type = "${var.instance_type}"
key_name = "${aws_key_pair.mytest-key.id}"
vpc_security_group_ids = ["${var.security_group}"]
subnet_id = "${element(var.subnet_mask, count.index )}"
tags {
Name = "my-test-server.${count.index + 1}"
resource "aws_key_pair" "mytest-key" {
key_name = "my-test-terraform-key"
public_key = "${file(var.my_public_key)}"
}
$ aws --region us-west-2 ec2 describe-images --owners aws-marketplace --filters Name=product-code,Values=aw0evgkw8e5c1q413zgy5pjce
{
"Images": [
{
"ProductCodes": [
{
"ProductCodeId": "aw0evgkw8e5c1q413zgy5pjce",
"ProductCodeType": "marketplace"
}
],
# To get the latest Centos7 AMI
data "aws_ami" "centos" {
owners = ["679593333241"]
most_recent = true
filter {
name = "name"
values = ["CentOS Linux 7 x86_64 HVM EBS *"]
}
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Deny",
"Action": [
"ec2:ModifyTransitGatewayVpcAttachment",
"ec2:DeleteTransitGatewayVpcAttachment",
"ec2:DetachClassicLinkVpc",
import boto3
# Enter the region your instances are in. Include only the region without specifying Availability Zone; e.g.; 'us-east-1'
region = 'XX-XXXXX-X'
# Enter your instances here: ex. ['X-XXXXXXXX', 'X-XXXXXXXX']
instances = ['X-XXXXXXXX']
def lambda_handler(event, context):
ec2 = boto3.client('ec2', region_name=region)
ec2.start_instances(InstanceIds=instances)
print 'started your instances: ' + str(instances)
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
import boto3
# Enter the region your instances are in. Include only the region without specifying Availability Zone; e.g., 'us-east-1'
region = 'XX-XXXXX-X'
# Enter your instances here: ex. ['X-XXXXXXXX', 'X-XXXXXXXX']
instances = ['X-XXXXXXXX']
def lambda_handler(event, context):
ec2 = boto3.client('ec2', region_name=region)
ec2.stop_instances(InstanceIds=instances)
print 'stopped your instances: ' + str(instances)
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
import json
import boto3
import sys
print('Loading function')
""" Function to define Lambda Handler """
def lambda_handler(event, context):
try:
client = boto3.client('cloudtrail')