Created
February 7, 2019 12:01
-
-
Save chefgs/925cf8a22ed5e317a04105f74122d374 to your computer and use it in GitHub Desktop.
Python Boto3 code for creating AWS Elastic Search Domain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Script creates the 'n' number of ES doamins | |
# Arg 1: AWS account profile name | |
# Arg 2: No of es domain to be created | |
import boto3 | |
import random | |
import sys | |
import json | |
# Fetch the account ID associated with the profile name passed as input arg | |
session = boto3.Session(profile_name=sys.argv[1]) | |
credentials = session.get_credentials() | |
ACCESS_KEY = credentials.access_key | |
SECRET_KEY = credentials.secret_key | |
client = boto3.client("sts", aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY) | |
account_id = client.get_caller_identity()["Account"] | |
print(account_id) | |
boto3.setup_default_session(profile_name=sys.argv[1]) | |
# Total number of ES domains to be created | |
count = int(sys.argv[2]) | |
for i in range(0, count): | |
es = boto3.client('es') | |
random_num = random.randint(0,3002) | |
es_name = 'demoes' + str(random_num) | |
print(es_name) | |
policy_data={ | |
"Version": "2012-10-17", | |
"Statement": [ | |
{ | |
"Effect": "Allow", | |
"Principal": { | |
"AWS": [ | |
"*" | |
] | |
}, | |
"Action": [ | |
"es:*" | |
], | |
"Resource": "arn:aws:es:us-west-2:"+account_id+":domain/"+es_name+"/*" | |
} | |
] | |
} | |
policy_json=json.dumps(policy_data) | |
# Create ES domain with minimum required params | |
es_data = es.create_elasticsearch_domain( | |
DomainName=str(es_name), | |
ElasticsearchVersion='2.3', | |
ElasticsearchClusterConfig={ | |
'InstanceType': 't2.micro.elasticsearch', | |
'InstanceCount': 1, | |
'DedicatedMasterEnabled': False, | |
'ZoneAwarenessEnabled': False | |
}, | |
EBSOptions={ | |
'EBSEnabled': True, | |
'VolumeType': 'standard', | |
'VolumeSize': 10 | |
}, | |
AccessPolicies=str(policy_json) | |
) | |
print ("Elastic Search: ", es_data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment