-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.py
More file actions
61 lines (45 loc) · 2.14 KB
/
Copy pathscript.py
File metadata and controls
61 lines (45 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import boto3
import time
# Configuración de LocalStack
localstack_endpoint = 'http://localhost.localstack.cloud:4566'
aws_access_key_id = "test"
aws_secret_access_key = "test"
aws_region = "us-east-1"
# Crear una tabla en DynamoDB en LocalStack
def create_dynamodb_table():
dynamodb = boto3.client('dynamodb', aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key, region_name=aws_region, endpoint_url=f'{localstack_endpoint}/dynamodb')
table_name = 'tabla-de-prueba'
attribute_definitions = [{'AttributeName': 'ID', 'AttributeType': 'N'}]
key_schema = [{'AttributeName': 'ID', 'KeyType': 'HASH'}]
provisioned_throughput = {'ReadCapacityUnits': 5, 'WriteCapacityUnits': 5}
response = dynamodb.create_table(
TableName=table_name,
AttributeDefinitions=attribute_definitions,
KeySchema=key_schema,
ProvisionedThroughput=provisioned_throughput
)
print(f'Tabla DynamoDB creada en LocalStack: {response["TableDescription"]["TableName"]}')
# Crear una cola en SQS en LocalStack
def create_sqs_queue():
sqs = boto3.client('sqs', aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key, region_name=aws_region, endpoint_url=f'{localstack_endpoint}/sqs')
queue_name = 'queue-one-py'
response = sqs.create_queue(QueueName=queue_name)
print(f'Cola SQS creada en LocalStack: {response["QueueUrl"]}')
# Lanzar una instancia EC2 en LocalStack
def launch_ec2_instance():
ec2 = boto3.client('ec2', aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key, region_name=aws_region, endpoint_url=f'{localstack_endpoint}/ec2')
ami_id = 'ami-ff0fea8310f3' # Reemplaza con un AMI válido
instance_type = 't3.nano'
response = ec2.run_instances(
ImageId=ami_id,
InstanceType=instance_type,
KeyName=key_name,
MinCount=1,
MaxCount=1
)
instance_id = response['Instances'][0]['InstanceId']
print(f'Instancia EC2 lanzada en LocalStack: {instance_id}')
# Llamar a las funciones para crear los recursos en LocalStack
create_dynamodb_table()
create_sqs_queue()
launch_ec2_instance()