· 6 years ago · Feb 08, 2019, 07:44 PM
1# Script creates the 'n' number of ES doamins
2# Arg 1: AWS account profile name
3# Arg 2: No of es domain to be created
4
5import boto3
6import random
7import sys
8import json
9
10# Fetch the account ID associated with the profile name passed as input arg
11session = boto3.Session(profile_name=sys.argv[1])
12credentials = session.get_credentials()
13ACCESS_KEY = credentials.access_key
14SECRET_KEY = credentials.secret_key
15
16client = boto3.client("sts", aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY)
17account_id = client.get_caller_identity()["Account"]
18print(account_id)
19
20boto3.setup_default_session(profile_name=sys.argv[1])
21
22# Total number of ES domains to be created
23count = int(sys.argv[2])
24
25for i in range(0, count):
26 es = boto3.client('es')
27 random_num = random.randint(0,3002)
28 es_name = 'demoes' + str(random_num)
29 print(es_name)
30 policy_data={
31 "Version": "2012-10-17",
32 "Statement": [
33 {
34 "Effect": "Allow",
35 "Principal": {
36 "AWS": [
37 "*"
38 ]
39 },
40 "Action": [
41 "es:*"
42 ],
43 "Resource": "arn:aws:es:us-west-2:"+account_id+":domain/"+es_name+"/*"
44 }
45 ]
46 }
47 policy_json=json.dumps(policy_data)
48
49 # Create ES domain with minimum required params
50 es_data = es.create_elasticsearch_domain(
51 DomainName=str(es_name),
52 ElasticsearchVersion='2.3',
53 ElasticsearchClusterConfig={
54 'InstanceType': 't2.micro.elasticsearch',
55 'InstanceCount': 1,
56 'DedicatedMasterEnabled': False,
57 'ZoneAwarenessEnabled': False
58 },
59 EBSOptions={
60 'EBSEnabled': True,
61 'VolumeType': 'standard',
62 'VolumeSize': 10
63 },
64 AccessPolicies=str(policy_json)
65 )
66 print ("Elastic Search: ", es_data)