Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add localstack #286

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ services:
- SESSION_SECRET=super_secret_session_key_change_me_in_production_please
- CI=
- NODE_ENV=development
- LOCALSTACK_HOSTNAME=localstack
jakekreider marked this conversation as resolved.
Show resolved Hide resolved
- EDGE_PORT=4566

db:
image: postgres:15-bookworm
Expand All @@ -37,6 +39,25 @@ services:
networks:
- redwood

localstack:
container_name: "cpf-reporter-localstack-main"
image: localstack/localstack
ports:
- "4566:4566" # LocalStack Gateway
- "4510-4559:4510-4559" # external services port range
environment:
- SERVICES=s3, iam, lambda, cloudwatch
- DEBUG=${DEBUG-}
- DOCKER_HOST=unix:///var/run/docker.sock
- AWS_DEFAULT_REGION=${AWS_REGION:-us-west-2}
- REPORTING_DATA_BUCKET_NAME=bucket
- AWS_ACCESS_KEY_ID="test"
- AWS_SECRET_ACCESS_KEY="test"
volumes:
- "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack"
- "/var/run/docker.sock:/var/run/docker.sock"
- "./entrypoint/init-aws.sh:/etc/localstack/init/ready.d/init-aws.sh"

# After starting with `docker compose -f ./docker-compose.dev.yml up`,
# use the console to run commands in the container:
#
Expand All @@ -59,6 +80,7 @@ services:
environment:
- DATABASE_URL=postgresql://redwood:redwood@db:5432/redwood
- TEST_DATABASE_URL=postgresql://redwood:redwood@db:5432/redwood_test
- LOCALSTACK_HOSTNAME=localstack
depends_on:
- db

Expand All @@ -77,6 +99,7 @@ services:
environment:
- DATABASE_URL=postgresql://redwood:redwood@db:5432/redwood
- TEST_DATABASE_URL=postgresql://redwood:redwood@db:5432/redwood_test
- LOCALSTACK_HOSTNAME=localstack
depends_on:
- db

Expand Down
84 changes: 84 additions & 0 deletions scripts/tests3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { v4 as uuidv4 } from "uuid"

Check failure on line 1 in scripts/tests3.ts

View workflow job for this annotation

GitHub Actions / qa / Lint JavaScript

`uuid` import should occur after import of `api/src/lib/aws`

Check warning on line 1 in scripts/tests3.ts

View workflow job for this annotation

GitHub Actions / qa / Lint JavaScript

Replace `"uuid"` with `'uuid'`
import {
CreateBucketCommand,
GetObjectCommand,
GetObjectCommandInput,
HeadObjectCommand,
HeadObjectCommandInput,
PutObjectCommand,
PutObjectCommandInput,
DeleteObjectCommand,
DeleteObjectCommandInput,
DeleteBucketCommand,
DeleteBucketCommandInput,
} from '@aws-sdk/client-s3'
import { getSignedUrl as awsGetSignedUrl } from '@aws-sdk/s3-request-presigner'
import { getPrismaClient } from 'api/src/lib/db'

Check failure on line 16 in scripts/tests3.ts

View workflow job for this annotation

GitHub Actions / qa / Lint JavaScript

`api/src/lib/db` import should occur after import of `api/src/lib/aws`
import AWS from 'api/src/lib/aws'

const REPORTING_DATA_BUCKET_NAME = `test-${process.env.REPORTING_DATA_BUCKET_NAME}-${uuidv4()}`

Check warning on line 19 in scripts/tests3.ts

View workflow job for this annotation

GitHub Actions / qa / Lint JavaScript

Replace `process.env.REPORTING_DATA_BUCKET_NAME` with `⏎··process.env.REPORTING_DATA_BUCKET_NAME⏎`

export default async () => {
const s3 = AWS.getS3Client()
await getPrismaClient()
const key = "12345"

Check warning on line 24 in scripts/tests3.ts

View workflow job for this annotation

GitHub Actions / qa / Lint JavaScript

Replace `"12345"` with `'12345'`

console.log("Create bucket")

Check warning on line 26 in scripts/tests3.ts

View workflow job for this annotation

GitHub Actions / qa / Lint JavaScript

Replace `"Create·bucket"` with `'Create·bucket'`
try {
await s3.send(new CreateBucketCommand({

Check warning on line 28 in scripts/tests3.ts

View workflow job for this annotation

GitHub Actions / qa / Lint JavaScript

Insert `⏎······`
Bucket: REPORTING_DATA_BUCKET_NAME,

Check warning on line 29 in scripts/tests3.ts

View workflow job for this annotation

GitHub Actions / qa / Lint JavaScript

Insert `··`
}))

Check warning on line 30 in scripts/tests3.ts

View workflow job for this annotation

GitHub Actions / qa / Lint JavaScript

Replace `····})` with `······})⏎····`
console.log("-- Bucket created")

Check warning on line 31 in scripts/tests3.ts

View workflow job for this annotation

GitHub Actions / qa / Lint JavaScript

Replace `"--·Bucket·created"` with `'--·Bucket·created'`
} catch (error) {
if (error.Code == "BucketAlreadyOwnedByYou") {

Check warning on line 33 in scripts/tests3.ts

View workflow job for this annotation

GitHub Actions / qa / Lint JavaScript

Replace `"BucketAlreadyOwnedByYou"` with `'BucketAlreadyOwnedByYou'`
console.log("-- Bucket already exists")

Check warning on line 34 in scripts/tests3.ts

View workflow job for this annotation

GitHub Actions / qa / Lint JavaScript

Replace `"--·Bucket·already·exists"` with `'--·Bucket·already·exists'`
} else {
console.log(error)
}
}

const baseParams: GetObjectCommandInput = {
Bucket: REPORTING_DATA_BUCKET_NAME,
Key: key,
}
try {
console.log("Create signed URL")
const url = await awsGetSignedUrl(s3, new GetObjectCommand(baseParams), {
expiresIn: 60,
})
console.log("-- signed url", url)

console.log("Upload file")
const uploadParams: PutObjectCommandInput = {
Body: "tests3.ts", // Upload this file as a test
Bucket: REPORTING_DATA_BUCKET_NAME,
Key: key,
}
await s3.send(new PutObjectCommand(uploadParams))

console.log("-- Look at uploaded file")
const headParams: HeadObjectCommandInput = {
Bucket: REPORTING_DATA_BUCKET_NAME,
Key: key,
}
const response = await s3.send(new HeadObjectCommand(headParams))
console.log("--", response)

console.log("Delete file")
const deleteParams: DeleteObjectCommandInput = {
Fixed Show fixed Hide fixed
Bucket: REPORTING_DATA_BUCKET_NAME,
Key: key,
}
await s3.send(new DeleteObjectCommand(deleteParams))
console.log("-- file deleted")
} catch (error) {
console.warn('Failed to test s3.')
console.error(error)
} finally {
console.log("Remove bucket")
await s3.send(new DeleteBucketCommand({
Bucket: REPORTING_DATA_BUCKET_NAME,
} as DeleteBucketCommandInput))
console.log("-- Bucket removed")
}
}
Loading