import boto3
ec2 = boto3.resource('ec2')
instance = ec2.create_instances(
ImageId='ami-xxxxxxxx',
MinCount=1,
MaxCount=1,
InstanceType='t2.micro'
)[0]
instance.wait_until_running()
print('Instance ID:', instance.id)
print('Public IP:', instance.public_ip_address)
import boto3
s3 = boto3.resource('s3')
bucket = s3.create_bucket(Bucket='my-bucket')
bucket.upload_file('myfile.txt', 'myfile.txt')
# playbook.yml
- name: Deploy Web App
hosts: web_servers
tasks:
- name: Install required packages
apt:
name: "{{ item }}"
state: present
with_items:
- nginx
- python3
- name: Copy web app files
copy:
src: /path/to/webapp
dest: /var/www/html
- name: Ensure NGINX is running
service:
name: nginx
state: started
# playbook.yml
- name: Configure SSH key login
hosts: all
tasks:
- name: Copy public key to authorized_keys
authorized_key:
user: "{{ ansible_user }}"
key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
# main.tf
provider "aws" {
access_key = "XXXXXXXXXXXXXXXXXXXX"
secret_key = "XXXXXXXXXXXXXXXXXXXX"
region = "us-west-2"
}
resource "aws_instance" "example" {
ami = "ami-xxxxxxxx"
instance_type = "t2.micro"
}
# main.tf
provider "aws" {
access_key = "XXXXXXXXXXXXXXXXXXXX"
secret_key = "XXXXXXXXXXXXXXXXXXXX"
region = "us-west-2"
}
resource "aws_s3_bucket" "example" {
bucket = "my-bucket"
}
请注意,在使用这些代码示例之前,你需要替换其中的占位符(如AMI ID、密钥等),以适应你的特定环境和需求。