feat(infra): add aws lambda pulumi deployment target

This commit is contained in:
2026-03-23 13:56:15 +04:00
parent 2688d66f33
commit ee8c53d89b
20 changed files with 2492 additions and 861 deletions

View File

@@ -0,0 +1,42 @@
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
const bucket = process.env.AWS_MINIAPP_BUCKET
const botApiUrl = process.env.BOT_API_URL
const awsRegion = process.env.AWS_REGION
const dryRun = process.env.AWS_PUBLISH_DRY_RUN === 'true'
if (!bucket) {
throw new Error('AWS_MINIAPP_BUCKET environment variable is required')
}
if (!botApiUrl) {
throw new Error('BOT_API_URL environment variable is required')
}
await Bun.$`bun run --filter @household/miniapp build`
const distDir = join(process.cwd(), 'apps/miniapp/dist')
const templatePath = join(process.cwd(), 'apps/miniapp/config.template.js')
const stagingDir = await mkdtemp(join(tmpdir(), 'household-miniapp-'))
try {
await Bun.$`cp -R ${distDir}/. ${stagingDir}`
const template = await Bun.file(templatePath).text()
const configScript = template.replace('${BOT_API_URL}', botApiUrl)
await Bun.write(join(stagingDir, 'config.js'), configScript)
const regionArgs = awsRegion ? ['--region', awsRegion] : []
const syncArgs = dryRun ? ['--dryrun'] : []
await Bun.$`aws s3 sync ${stagingDir} s3://${bucket} --delete --exclude index.html --exclude config.js --cache-control public,max-age=31536000,immutable ${regionArgs} ${syncArgs}`
await Bun.$`aws s3 cp ${join(stagingDir, 'index.html')} s3://${bucket}/index.html --cache-control no-cache ${regionArgs} ${syncArgs}`
await Bun.$`aws s3 cp ${join(stagingDir, 'config.js')} s3://${bucket}/config.js --cache-control no-cache ${regionArgs} ${syncArgs}`
} finally {
await rm(stagingDir, {
recursive: true,
force: true
})
}

View File

@@ -0,0 +1,38 @@
const repositoryUrl = process.env.AWS_ECR_REPOSITORY_URL
const imageTag = process.env.AWS_ECR_IMAGE_TAG ?? 'latest'
const awsRegion = process.env.AWS_REGION
if (!repositoryUrl) {
throw new Error('AWS_ECR_REPOSITORY_URL environment variable is required')
}
if (!awsRegion) {
throw new Error('AWS_REGION environment variable is required')
}
const imageRef = `${repositoryUrl}:${imageTag}`
const passwordProcess = Bun.spawnSync(['aws', 'ecr', 'get-login-password', '--region', awsRegion], {
stdout: 'pipe',
stderr: 'inherit'
})
if (passwordProcess.exitCode !== 0) {
throw new Error('Failed to obtain an ECR login password')
}
const loginProcess = Bun.spawnSync(
['docker', 'login', '--username', 'AWS', '--password-stdin', repositoryUrl.split('/')[0]!],
{
stdin: passwordProcess.stdout,
stdout: 'inherit',
stderr: 'inherit'
}
)
if (loginProcess.exitCode !== 0) {
throw new Error('Failed to login to ECR')
}
await Bun.$`docker build -f apps/bot/Dockerfile.lambda -t ${imageRef} .`
await Bun.$`docker push ${imageRef}`