import json
import boto3
import time

ec2 = boto3.client('ec2')
ssm = boto3.client('ssm')

user_data = """#!bin/bash
sudo su -
export HOME=~
cd ~
sleep 240
curl -LOJ https://github.com/crypto-org-chain/chain-main/releases/download/v1.2.1/chain-main_1.2.1_Linux_x86_64.tar.gz
tar -zxvf chain-main_1.2.1_Linux_x86_64.tar.gz
cd bin/
./chain-maind version
./chain-maind init [moniker] --chain-id crypto-org-chain-mainnet-1
curl https://raw.githubusercontent.com/crypto-org-chain/mainnet/main/crypto-org-chain-mainnet-1/genesis.json > ~/.chain-maind/config/genesis.json
if [[ $(sha256sum ~/.chain-maind/config/genesis.json | awk '{print $1}') = "d299dcfee6ae29ca280006eaa065799552b88b978e423f9ec3d8ab531873d882" ]]; then echo "OK"; else echo "MISMATCHED"; fi;
sed -i.bak -E 's#^(minimum-gas-prices[[:space:]]+=[[:space:]]+)""$#\1"0.025basecro"#' ~/.chain-maind/config/app.toml
sed -i.bak -E 's#^(seeds[[:space:]]+=[[:space:]]+).*$#\1"8dc1863d1d23cf9ad7cbea215c19bcbe8bf39702@p2p.baaa7e56-cc71-4ae4-b4b3-c6a9d4a9596a.cryptodotorg.bison.run:26656,494d860a2869b90c458b07d4da890539272785c9@p2p.fabc23d9-e0a1-4ced-8cd7-eb3efd6d9ef3.cryptodotorg.bison.run:26656,8a7922f3fb3fb4cfe8cb57281b9d159ca7fd29c6@p2p.aef59b2a-d77e-4922-817a-d1eea614aef4.cryptodotorg.bison.run:26656,dc2540dabadb8302da988c95a3c872191061aed2@p2p.7d1b53c0-b86b-44c8-8c02-e3b0e88a4bf7.cryptodotorg.herd.run:26656,33b15c14f54f71a4a923ac264761eb3209784cf2@p2p.0d20d4b3-6890-4f00-b9f3-596ad3df6533.cryptodotorg.herd.run:26656,d2862ef8f86f9976daa0c6f59455b2b1452dc53b@p2p.a088961f-5dfd-4007-a15c-3a706d4be2c0.cryptodotorg.herd.run:26656,87c3adb7d8f649c51eebe0d3335d8f9e28c362f2@seed-0.crypto.org:26656,e1d7ff02b78044795371beb1cd5fb803f9389256@seed-1.crypto.org:26656,2c55809558a4e491e9995962e10c026eb9014655@seed-2.crypto.org:26656"#' ~/.chain-maind/config/config.toml
sed -i.bak -E 's#^(create_empty_blocks_interval[[:space:]]+=[[:space:]]+).*$#\1"5s"#' ~/.chain-maind/config/config.toml
./chain-maind start
"""

'''
Please add your values in here
'''
subnetid = <subnetid>
acccountid = <your accountid>
iamrole = <your iam role>
'''
Please add your values in here
'''

iamarn = arn:aws:iam::acccountid:instance-profile/iamrole

def lambda_handler(event, context):

    createinstance = ec2.run_instances(
        ImageId='ami-0f511ead81ccde020',
        InstanceType='t2.micro',
        MaxCount=1,
        MinCount=1,
        IamInstanceProfile={
        'Arn': iamarn
        },
        UserData=user_data,
        NetworkInterfaces=[
            {
            'AssociatePublicIpAddress': True,
            'DeviceIndex':0,
            'SubnetId': subnetid
            }
        ]
    )
    instanceid=createinstance['Instances'][0]['InstanceId']
    print(instanceid)
