Remote development on EC2 instances

--Resource --

1. Create a bot


# Open this bot in telegram

https://telegram.me/botfather

# give the commands

/start

/newbot

# give a name to your bot
# you will get a token . SAVE IT!

2. Create EC2 instance


# open aws account
# search EC2
# Launch new insatnce (If you want to control an intance already created , ignore this step)


3. Prepare zip file


   # Open a linux terminal and give this commans to create a lambda function

    mkdir /tmp/bot

    cd /tmp/bot

    pip3 install python-telegram-bot pynamodb --system -t .

    wget https://raw.githubusercontent.com/itpp-labs/odoo-devops-docs/master/tools/ec2-dev-bot/lambda_function.py -O lambda_function.py
    
    rm -rf botocore* tornado* docutils*

    zip -r /tmp/bot.zip *      

    # save this zip file 

4. Create Lambda function


# open aws account
# search lambda
# click on create function
# choose Author from scratch , give function name , select runtime Python 3.8
# click on create function

5. Permissions (Role)


# open aws account
# search IAM service
# select Acces Management -> Policies (on left side)
# create policy

    Create policy of actions for DynamoDB:

        Service – DynamoDB
        Action – All DynamoDB actions
        Resources – All Resources

    Create policy of actions for EC2:

        Service – EC2
        Action – All EC2 actions
        Resources – All Resources

# search IAM service
# select Acces Management -> roles (on left side)
# Open role attached to the lambda function
# Attache created policies

5. Upload code


# open asw account
# search lambda
# select the function you were created
# Code entry type: Upload a .zip file
# Upload bot.zip

6. Time OUT


# open asw account
# search lambda
# select the function you were created
# select configuration
# select General configuration
# set Timeout 35sec

7. Trigger


# open aws account
# search lambda
# select the function you were created
# select configuration
# select Triggers
# set API Gateway

    API type: HTTP
    Authorization: NONE
    Method: ANY

# set CloudWatch Events

    Rule name           –> ec2-dev-bot-cron
    Schedule expression –> rate(1 hour)

8. Register webhook at telegram


# open a linux terminal and type it
# AWS_API_GATEWAY (HOw get?) 
    open aws account
    search lambda
    select the function you were created
    select configuration
    select Triggers
    select API Gateway
    API endpoint = AWS_API_GATEWAY

# Telegram token from https://telegram.me/botfather when you create a bot


AWS_API_GATEWAY=XXX
TELEGRAM_TOKEN=XXX
curl -XPOST https://api.telegram.org/bot$TELEGRAM_TOKEN/setWebhook --data "url=$AWS_API_GATEWAY" --data "allowed_updates=['message','callback_query']"
 
OR use this method to set up hook instead of use commands in terminal

https://api.telegram.org/bot{my_bot_token}/setWebhook?url={url_to_send_updates_to}
https://api.telegram.org/bot{my_bot_token}/getWebhookInfo

9. Settings


# open aws account
# search lambda
# select the function you were created
# select configuration
# select Environment variables
# set these variables
#NOTE
# replace 123 -> your telegram user id (You can get one via Get My ID bot)
# Instance ID looks like i-07e6... and can be found in Description tab of existing Instance
    LOG_LEVEL           =    DEBUG
    AUTO_SHUTDOWN_HARD  =   2
    AUTO_SHUTDOWN_SOFT  =   2
    USER_123_INSTANCE   =   (aws instance id)
    USER_123_CODE       =   (123)
    TELEGRAM_TOKEN      =   (new bot token)
    DOMAIN=”USERCODE.example.com”
    DOMAIN_NO_SSL=”USERCODE.nossl.example.com”

10. How to use


# open telegram bot created by you

get instance info
/status  

turn the instance off after confirmation
/shutdown  

turn the instance off without confirmation
Shutdown 

turn on instance or extend time to shutdown
/up 

10. Usefull things


# Reset webhook
https://api.telegram.org/bot{my_bot_token}/setWebhook?url=

# To view the messages from your BOT
https://api.telegram.org/bot{my_bot_token}/getUpdates