Showing posts with label Boto3. Show all posts
Showing posts with label Boto3. Show all posts

Thursday, 9 March 2023

Create a VPC using the Boto3 library in Python

>> How to Install Python and Boto3 

1. What is VPC in AWS
- A VPC (Virtual Private Cloud) in AWS is a virtual network infrastructure that allows you to create a private and isolated virtual network in the AWS cloud.
- With a VPC, you can launch Amazon Elastic Compute Cloud (EC2) instances, create subnets, configure route tables, and apply security measures to control access to your resources.
- A VPC enables you to create your own private cloud within the AWS infrastructure, giving you complete control over your network configuration. Some of the key features and benefits of using VPCs in AWS include security, flexibility, scalability, availability, and cost-effectiveness.

2. Create VPC using the Boto3 library in Python  

- Full sources code in Github >> aws-python-boto3

# File 001_BuildVpc.py

- createClientSession()

- createVpc()

- deleteVpc()

import os
import boto3
from dotenv import load_dotenv
from Common import saveData, getData

data = getData()


# ====================
def createClientSession():
# config credentials from .env file
load_dotenv()
AWS_ACCESS_KEY = os.environ.get("AWS_ACCESS_KEY")
AWS_SECRET_KEY = os.environ.get("AWS_SECRET_KEY")
REGION_NAME = os.environ.get("REGION_NAME")

# or config credentials directly
# AWS_ACCESS_KEY = xxxxxxxxxxxxxxxx
# AWS_SECRET_KEY = yyyyyyyyyyyyyyyy
# REGION_NAME = zzzzzzzzzzzzzzzz

# create session with the credentials
session = boto3.Session(
aws_access_key_id=os.environ.get("AWS_ACCESS_KEY"),
aws_secret_access_key=os.environ.get("AWS_SECRET_KEY"),
region_name=os.environ.get("REGION_NAME"),
)
ec2Client = session.client("ec2")

return ec2Client


# ====================
def createVpc(ec2):
# create the VPC
vpc_name = "Your Vpc Name"
response = ec2.create_vpc(
CidrBlock="10.0.0.0/16",
AmazonProvidedIpv6CidrBlock=True,
InstanceTenancy="default",
TagSpecifications=[
{
"ResourceType": "vpc",
"Tags": [
{"Key": "Name", "Value": vpc_name},
],
},
],
)

print("################\ncreate_vpc", response)

# get the ID of the new VPC
data["vpcId"] = response["Vpc"]["VpcId"]
print("Created VPC with ID:", data["vpcId"])
saveData(data)


# ====================
def deleteVpc(ec2):
# delete the VPC
data = getData()
response = ec2.delete_vpc(VpcId=data["vpcId"])
print("################\ndelete_vpc", response)


#####################
if __name__ == "__main__":
ec2Client = createClientSession()
createVpc(ec2Client)
# deleteVpc(ec2Client)

+ To create VPC run:

python3 001_BuildVpc.py

+ To delete VPC comment #createVpc(), uncomment deleteVpc() and run:

python3 001_BuildVpc.py

References:
https://aws.amazon.com/sdk-for-python/
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2/client/create_vpc.html

Thank you!

Wednesday, 8 March 2023

How to Install Python and Boto3

>> Create a VPC using the Boto3 library in Python  

1. Install Python3:
- Go to the Python website at https://www.python.org/downloads/
- Click on the "Download" button for the latest version of Python
- Choose the appropriate installer for your operating system (Windows, Mac, or Linux)
- Follow the installation wizard to install Python on your computer. Be sure to select the option to add Python to your PATH environment variable.

2. Install Boto3:
- Open a command prompt or terminal window on your computer
- Type the following command:

pip3 install boto3

- Press Enter to execute the command
- Wait for the installation to complete. This may take a few minutes depending on your internet connection speed.

3. Verify the installation:
- Type the following command in the command prompt or terminal window:

python3 -c "import boto3; print(boto3.__version__)"

- If the installation was successful, you should see the version number of Boto3 printed on the screen.

Now, we can now start using Python and Boto3 to interact with AWS services.



Publish npm package

  Để publish   pav-kit  lên NPM, bạn hãy làm theo các bước dưới đây. Tôi đã tạo thêm file  index.js  để đảm bảo gói tin hợp lệ. Bước 1: Tạo ...