Thursday, March 3, 2022

Configure SNS service on AWS

To send sms in india, sns service should be configured in Singapore. Mumbai is not providing facility right now. 

To send sms in US, service should be configured in US region.

1) Open SNS in AWS Management Console.  

Create Topic (Topic name + Display name > create topic)

2) Create Subscription (ARN : Created Topic Name + Protocol : Email > Create subscription)

3) Create user in IAM and attach policy SNS full access.

4) Now try sending sms using python script 

pip3 install boto3

nano sms.py

    import boto3
    # Create an SNS client
    access_key = 'ACLAXXXXXXXXXXXXXZBA'
    secret = 'Zwxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    region = "ap-southeast-1"
    number = '+9199xxxxxxxx'
    sender_id = 'senderidname'
    sms_message = 'Your code: 123456'
    sns = boto3.client('sns', aws_access_key_id=access_key, aws_secret_access_key=secret, region_name=region)
    sns.publish(PhoneNumber=number, Message=sms_message, MessageAttributes={'AWS.SNS.SMS.SenderID': {'DataType': 'String', 'StringValue':     sender_id}, 'AWS.SNS.SMS.SMSType': {'DataType': 'String', 'StringValue': 'Transactional'}})
python sms.py 

5) If you do not receive sms then check error in cloudwatch but before it you need to enable the logs.
Click on Text Messaging (SMS) option > Text messaging preferences > Edit
Delivery status logging - optional > Create new service role

It will take you on IAM page and it will create roles for
SNS Success feedback and SNS Failure feedback > Allow

Once roles are created

Delivery status logging - optional > Use existing service role > add ARN of failure feedback
arn:aws:iam::360209543005:role/SNSFailureFeedback > save changes

Now errors will be reported in cloudwatch log group
check the error > cloudwatch > Log groups

 "delivery":
        "phoneCarrier": "Vodaphone - Kerela",
        "mnc": 90,
        "numberOfMessageParts": 1,
        "destination": "+9199xxxxxxxx",
        "priceInUSD": 0.00223,
        "smsType": "Promotional",
        "mcc": 404,
        "providerResponse": "Phone carrier has blocked this message",
        "dwellTimeMs": 172,
        "dwellTimeMsUntilDeviceAck": 669
    },
    "status": "FAILURE"
Phone carrier has blocked the sms that's why it is not delivered because you were sending promotional sms type.
You should have sent Transaction sms type.
Either select default sms type transactional
Click on Text Messaging (SMS) option > Text messaging preferences > Edit > Default message type > transactional
OR send message type in the code, just like above example did. Now message will be delivered.

Send message by sender ID
i) US does not support sender ID
ii) To send message in India by sender id
Generate request in support for sender ID.
https://docs.aws.amazon.com/pinpoint/latest/userguide/channels-sms-awssupport-sender-id.html

Saturday, February 5, 2022

Ansible - Run a task on multiple linux servers simultaneously

If you want to install a package or run a command on multiple servers, Ansible is a smart way to do the thing.

Suppose there are 100 Ubuntu servers and you want to perform a task on all the servers simultaneously, suppose you want to install java 11 on all the servers or run a shell script on all the servers, here is the way.

 After installing Ansible on your linux server, update inventory file of your linux server i.e. /etc/ansible/hosts 

Now add entries of your hundred servers i.e. IP, server login username and ssh key file path.

host1 ansible_ssh_host=23.56.xx.xx ansible_ssh_private_key_file=/path/of/ssh_key.pem ansible_ssh_user=ubuntu

host2 ansible_ssh_host=65.xx.xx.xx ansible_ssh_private_key_file=~/.ssh/ssh_key.pem ansible_ssh_user=ubuntu

host3 ansible_ssh_host=65.xx.xx.xx ansible_ssh_private_key_file=~/.ssh/ssh_key.pem ansible_ssh_user=ubuntu

 Suppose this is the shell script which should be run all 100 Ubuntu servers.

    #!/bin/bash

sudo apt-get update

sudo apt-get install -y openjdk-11-jdk

Now run the command

ansible all -i /etc/ansible/hosts -a "bash ~/Documents/script.sh"
It will install the java 11 on all 100 servers.

This time is surely less than the time taking log into the each server and run the command.

B) Ansible - Run a command on all servers

ansible all -i /etc/ansible/hosts -m command -a 'sudo apt-get update' 

Sunday, January 16, 2022

Ubuntu - Switch between multiple php versions on web and on command line

Multiple php versions are installed on ubuntu. You can find the config files of all the versions in /etc/php.

If you check the version on command line using

php --version

This version may be or may not be same as php version on browser (using apache2). You use phpinfo(); function to find the phpversion. You might have installed multiple php versions using apt-get install.

To shift different version on web using apache2

First disable the current enabled version using command

sudo a2dismod php7.0

then enable the other version which you want to use on web

sudo a2enmod php8.0

sudo service apache2 restart

To shift different version on terminal

sudo update-alternatives --set php /usr/bin/php8.0