implement pulumi stack base

This commit is contained in:
falsycat 2025-06-14 13:07:32 +09:00
parent a304829a75
commit 65ca4fcb3e
7 changed files with 209 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.pyc
venv/

2
Pulumi.dev.yaml Normal file
View File

@ -0,0 +1,2 @@
config:
aws:region: ap-northeast-3

11
Pulumi.yaml Normal file
View File

@ -0,0 +1,11 @@
name: imbusy
description: task management app
runtime:
name: python
options:
toolchain: pip
virtualenv: venv
config:
pulumi:tags:
value:
pulumi:template: aws-python

92
README.md Normal file
View File

@ -0,0 +1,92 @@
# AWS Python S3 Bucket Pulumi Template
A minimal Pulumi template for provisioning a single AWS S3 bucket using Python.
## Overview
This template provisions an S3 bucket (`pulumi_aws.s3.BucketV2`) in your AWS account and exports its ID as an output. Its an ideal starting point when:
- You want to learn Pulumi with AWS in Python.
- You need a barebones S3 bucket deployment to build upon.
- You prefer a minimal template without extra dependencies.
## Prerequisites
- An AWS account with permissions to create S3 buckets.
- AWS credentials configured in your environment (for example via AWS CLI or environment variables).
- Python 3.6 or later installed.
- Pulumi CLI already installed and logged in.
## Getting Started
1. Generate a new project from this template:
```bash
pulumi new aws-python
```
2. Follow the prompts to set your project name and AWS region (default: `us-east-1`).
3. Change into your project directory:
```bash
cd <project-name>
```
4. Preview the planned changes:
```bash
pulumi preview
```
5. Deploy the stack:
```bash
pulumi up
```
6. Tear down when finished:
```bash
pulumi destroy
```
## Project Layout
After running `pulumi new`, your directory will look like:
```
├── __main__.py # Entry point of the Pulumi program
├── Pulumi.yaml # Project metadata and template configuration
├── requirements.txt # Python dependencies
└── Pulumi.<stack>.yaml # Stack-specific configuration (e.g., Pulumi.dev.yaml)
```
## Configuration
This template defines the following config value:
- `aws:region` (string)
The AWS region to deploy resources into.
Default: `us-east-1`
View or update configuration with:
```bash
pulumi config get aws:region
pulumi config set aws:region us-west-2
```
## Outputs
Once deployed, the stack exports:
- `bucket_name` — the ID of the created S3 bucket.
Retrieve outputs with:
```bash
pulumi stack output bucket_name
```
## Next Steps
- Customize `__main__.py` to add or configure additional resources.
- Explore the Pulumi AWS SDK: https://www.pulumi.com/registry/packages/aws/
- Break your infrastructure into modules for better organization.
- Integrate into CI/CD pipelines for automated deployments.
## Help and Community
If you have questions or need assistance:
- Pulumi Documentation: https://www.pulumi.com/docs/
- Community Slack: https://slack.pulumi.com/
- GitHub Issues: https://github.com/pulumi/pulumi/issues
Contributions and feedback are always welcome!

98
__main__.py Normal file
View File

@ -0,0 +1,98 @@
"""An AWS Python Pulumi program"""
import mimetypes
import os
import pulumi
import pulumi_aws as aws
project = pulumi.get_project()
stack = pulumi.get_stack()
prefix = f"{project}-{stack}"
TAGS = {
"project": project,
"env": stack,
}
bucket = aws.s3.Bucket(
f"{prefix}-site-bucket",
tags = TAGS,
website = aws.s3.BucketWebsiteArgs(
index_document = "index.html"
),
)
oai = aws.cloudfront.OriginAccessIdentity(
f"{prefix}-oai",
)
bucket_policy = aws.s3.BucketPolicy(
f"{prefix}-site-bucket-policy",
bucket = bucket.id,
policy = pulumi.Output.all(oai.iam_arn, bucket.id).apply(lambda args: f"""{{
"Version": "2012-10-17",
"Statement": [
{{
"Effect": "Allow",
"Principal": {{
"AWS": "{args[0]}"
}},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::{args[1]}/*"
}}
]
}}""")
)
cdn = aws.cloudfront.Distribution(
f"{prefix}-cdn-distribution",
tags = TAGS,
origins=[{
"domain_name": bucket.bucket_regional_domain_name,
"origin_id": bucket.arn,
"s3_origin_config": {
"origin_access_identity": oai.cloudfront_access_identity_path,
}
}],
enabled=True,
default_root_object="index.html",
default_cache_behavior=aws.cloudfront.DistributionDefaultCacheBehaviorArgs(
allowed_methods=["GET", "HEAD"],
cached_methods=["GET", "HEAD"],
target_origin_id=bucket.arn,
viewer_protocol_policy="redirect-to-https",
forwarded_values=aws.cloudfront.DistributionDefaultCacheBehaviorForwardedValuesArgs(
query_string=False,
cookies=aws.cloudfront.DistributionDefaultCacheBehaviorForwardedValuesCookiesArgs(
forward="none",
),
),
),
price_class="PriceClass_100",
restrictions=aws.cloudfront.DistributionRestrictionsArgs(
geo_restriction=aws.cloudfront.DistributionRestrictionsGeoRestrictionArgs(
restriction_type="none",
),
),
viewer_certificate=aws.cloudfront.DistributionViewerCertificateArgs(
cloudfront_default_certificate=True,
),
)
def upload_directory(directory_path, bucket_name):
for root, dirs, files in os.walk(directory_path):
for file in files:
file_path = os.path.join(root, file)
relative_path = os.path.relpath(file_path, directory_path)
content_type, _ = mimetypes.guess_type(file_path)
aws.s3.BucketObject(
relative_path,
bucket=bucket_name,
source=pulumi.FileAsset(file_path),
content_type=content_type or "application/octet-stream",
key=relative_path,
)
upload_directory("./www", bucket.id)
pulumi.export("bucketName", bucket.bucket)
pulumi.export("cloudFrontDomain", cdn.domain_name)

2
requirements.txt Normal file
View File

@ -0,0 +1,2 @@
pulumi>=3.0.0,<4.0.0
pulumi-aws>=6.0.2,<7.0.0

2
www/index.html Normal file
View File

@ -0,0 +1,2 @@
helloworld
foobazbar