Created
July 11, 2025 07:27
-
-
Save flaneur2020/7b6b24dff76d646afc7870ec74b23937 to your computer and use it in GitHub Desktop.
iac sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pulumi | |
| import pulumi_aws as aws | |
| import pulumi_awsx as awsx | |
| import pulumi_eks as eks | |
| # --- 1. 创建网络 (VPC) --- | |
| # 使用 pulumi_awsx 创建一个 VPC。这个高级组件会自动创建公有和私有子网、 | |
| # Internet Gateway、NAT Gateway 等,大大简化了网络配置。 | |
| vpc = awsx.ec2.Vpc("my-app-vpc", | |
| number_of_availability_zones=2, # 在两个可用区中创建资源以实现高可用 | |
| tags={ | |
| "Project": "my-app", | |
| }) | |
| # --- 2. 创建 EKS Kubernetes 集群 --- | |
| # 使用 pulumi_eks 创建一个 EKS 集群。 | |
| # 它会自动利用上面创建的 VPC 和子网。 | |
| cluster = eks.Cluster("my-app-eks", | |
| # 指定集群所在的 VPC | |
| vpc_id=vpc.vpc_id, | |
| # 将控制平面和 Worker Nodes 部署在这些子网中 | |
| public_subnet_ids=vpc.public_subnet_ids, | |
| private_subnet_ids=vpc.private_subnet_ids, | |
| # 为 Worker Nodes 指定实例类型和数量 | |
| instance_type="t3.medium", | |
| desired_capacity=2, | |
| min_size=1, | |
| max_size=3, | |
| # 指定 Kubernetes 版本 | |
| version="1.27", | |
| tags={ | |
| "Project": "my-app", | |
| }) | |
| # --- 3. 创建 RDS MySQL 数据库 --- | |
| # a. 创建一个数据库子网组,告诉 RDS 应该在哪些子网中创建数据库实例。 | |
| # 我们使用私有子网来保护数据库。 | |
| db_subnet_group = aws.rds.SubnetGroup("my-app-db-subnet-group", | |
| subnet_ids=vpc.private_subnet_ids, | |
| tags={ | |
| "Name": "My App DB Subnet Group", | |
| }) | |
| # b. 创建一个安全组 (Security Group) 来控制对数据库的访问。 | |
| db_security_group = aws.ec2.SecurityGroup("my-app-db-sg", | |
| vpc_id=vpc.vpc_id, | |
| description="Allow traffic from EKS nodes to the database", | |
| # 定义入站规则 (Ingress) | |
| ingress=[ | |
| aws.ec2.SecurityGroupIngressArgs( | |
| protocol="tcp", | |
| from_port=3306, # MySQL 默认端口 | |
| to_port=3306, | |
| # 关键点:只允许来自 EKS Worker Nodes 安全组的流量。 | |
| # 这是连接 EKS 和 RDS 的最安全方式。 | |
| security_groups=[cluster.node_security_group_id], | |
| ) | |
| ], | |
| tags={ | |
| "Name": "my-app-db-sg", | |
| }) | |
| # c. 创建 RDS MySQL 实例 | |
| # **重要改动**: 我们在这里省略了 `password` 参数。 | |
| # AWS 将会自动为我们生成一个安全的随机密码。 | |
| db_instance = aws.rds.Instance("my-app-db", | |
| engine="mysql", | |
| engine_version="8.0", | |
| instance_class="db.t3.micro", # 使用一个较小的实例类型用于演示 | |
| allocated_storage=20, # 20 GB 存储空间 | |
| db_name="myappdb", | |
| username="admin", | |
| # password 参数已被移除 | |
| db_subnet_group_name=db_subnet_group.name, | |
| vpc_security_group_ids=[db_security_group.id], | |
| # 对于生产环境,应设置为 False 或省略。设置为 True 可以在销毁时跳过创建最终快照,方便演示。 | |
| skip_final_snapshot=True, | |
| # 确保数据库不被公开访问 | |
| publicly_accessible=False, | |
| tags={ | |
| "Project": "my-app", | |
| }) | |
| # --- 4. 导出输出 --- | |
| # 导出关键信息,方便后续使用。 | |
| # 导出 EKS 集群的 Kubeconfig 文件,以便使用 kubectl 连接。 | |
| # Kubeconfig 包含敏感信息,所以我们将其标记为 secret。 | |
| pulumi.export("kubeconfig", pulumi.Output.secret(cluster.kubeconfig)) | |
| # 导出集群的名称和端点 | |
| pulumi.export("cluster_name", cluster.eks_cluster.name) | |
| pulumi.export("cluster_endpoint", cluster.eks_cluster.endpoint) | |
| # 导出 RDS 数据库的连接信息 | |
| pulumi.export("db_endpoint", db_instance.endpoint) | |
| pulumi.export("db_port", db_instance.port) | |
| pulumi.export("db_name", db_instance.db_name) | |
| # **新增输出**: 导出由 AWS 生成的数据库密码。 | |
| # db_instance.password 是一个 Output<string>,它包含生成的密码。 | |
| # Pulumi 会自动将其识别为机密并加密存储。 | |
| pulumi.export("db_generated_password", pulumi.Output.secret(db_instance.password)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment