Skip to content

Deno on AWS with SST

SST를 사용하여 Deno 앱을 생성하고 AWS에 배포하기

AWS에서 SST로 Deno 사용하기

Deno로 앱을 만들고, 파일 업로드를 위한 S3 버킷을 추가한 다음, SST를 사용해 AWS에 컨테이너로 배포해 보겠습니다.

시작하기 전에 AWS 자격 증명을 설정하세요.


예제

여러분이 참고할 수 있는 다른 Deno 예제도 몇 가지 있습니다.


1. 프로젝트 생성

Deno 앱을 만들어 보겠습니다.

Terminal window
deno init aws-deno

Deno 서버 초기화

main.ts 파일을 아래 코드로 교체하세요.

main.ts
Deno.serve(async (req) => {
const url = new URL(req.url);
if (url.pathname === "/" && req.method === "GET") {
return new Response("Hello World!");
}
return new Response("404!");
});

이 코드는 기본적으로 포트 8000에서 HTTP 서버를 시작합니다.

SST 초기화

SST를 전역으로 설치했는지 확인하세요.

Terminal window
sst init

이 명령어는 프로젝트 루트에 sst.config.ts 파일을 생성합니다.


2. 서비스 추가하기

Deno 앱을 배포하기 위해 Amazon ECS와 함께 AWS Fargate 컨테이너를 추가해 보겠습니다. sst.config.ts 파일을 업데이트하세요.

sst.config.ts
async run() {
const vpc = new sst.aws.Vpc("MyVpc");
const cluster = new sst.aws.Cluster("MyCluster", { vpc });
cluster.addService("MyService", {
loadBalancer: {
ports: [{ listen: "80/http", forward: "8000/http" }],
},
dev: {
command: "deno task dev",
},
});
}

이 코드는 VPC와 ECS 클러스터를 생성하고, 여기에 Fargate 서비스를 추가합니다.

dev.command는 SST에게 개발 모드에서 로컬로 Deno 앱을 실행하도록 지시합니다.


개발 모드 시작

개발 모드를 시작하려면 다음 명령어를 실행하세요. 이 명령은 SST와 여러분의 Deno 앱을 시작합니다.

Terminal window
sst dev

실행이 완료되면 사이드바에서 MyService를 클릭하고 브라우저에서 Deno 앱을 열어보세요.

3. S3 버킷 추가하기

파일 업로드를 위해 S3 버킷을 추가해 보겠습니다. Vpc 컴포넌트 아래에 다음 코드를 sst.config.ts 파일에 추가하세요.

sst.config.ts
const bucket = new sst.aws.Bucket("MyBucket");

버킷 연결하기

이제 버킷을 컨테이너에 연결합니다.

sst.config.ts
cluster.addService("MyService", {
// ...
link: [bucket],
});

이렇게 하면 Deno 앱에서 버킷을 참조할 수 있습니다.

4. 파일 업로드

/ 라우트로 POST 요청을 보내 S3 버킷에 파일을 업로드하려고 합니다. main.ts 파일의 Hello World 라우트 아래에 이 코드를 추가해 보겠습니다.

main.ts
if (url.pathname === "/" && req.method === "POST") {
const formData: FormData = await req.formData();
const file: File | null = formData?.get("file") as File;
const params = {
Bucket: Resource.MyBucket.name,
ContentType: file.type,
Key: file.name,
Body: file,
};
const upload = new Upload({
params,
client: s3,
});
await upload.done();
return new Response("파일 업로드가 완료되었습니다.");
}

필요한 모듈을 임포트합니다. 아래 추가 모듈도 사용할 예정입니다.

main.ts
import { Resource } from "sst";
import {
S3Client,
GetObjectCommand,
ListObjectsV2Command,
} from "@aws-sdk/client-s3";
import { Upload } from "@aws-sdk/lib-storage";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
const s3 = new S3Client();

그리고 npm 패키지를 설치합니다.

Terminal window
deno install npm:sst npm:@aws-sdk/client-s3 npm:@aws-sdk/lib-storage npm:@aws-sdk/s3-request-presigner

5. 파일 다운로드

S3 버킷에서 가장 최신 파일을 다운로드할 수 있는 /latest 라우트를 추가하겠습니다. main.ts 파일의 업로드 라우트 아래에 이 코드를 추가합니다.

main.ts
if (url.pathname === "/latest" && req.method === "GET") {
const objects = await s3.send(
new ListObjectsV2Command({
Bucket: Resource.MyBucket.name,
}),
);
const latestFile = objects.Contents!.sort(
(a, b) =>
(b.LastModified?.getTime() ?? 0) - (a.LastModified?.getTime() ?? 0),
)[0];
const command = new GetObjectCommand({
Key: latestFile.Key,
Bucket: Resource.MyBucket.name,
});
return Response.redirect(await getSignedUrl(s3, command));
}

앱 테스트하기

파일을 업로드하려면 프로젝트 루트에서 다음 명령어를 실행하세요. 사이드바의 MyService 탭으로 이동해 Deno 권한 요청을 승인해야 할 수도 있습니다.

Terminal window
curl -F file=@deno.json http://localhost:8000/

이 명령어는 deno.json 파일을 업로드합니다. 이제 브라우저에서 http://localhost:8000/latest로 이동하면 방금 업로드한 내용을 확인할 수 있습니다.


5. 앱 배포하기

앱을 배포하기 위해 먼저 Dockerfile을 추가합니다.

Dockerfile
FROM denoland/deno
EXPOSE 8000
USER deno
WORKDIR /app
ADD . /app
RUN deno install --entrypoint main.ts
CMD ["run", "--allow-all", "main.ts"]

이제 Docker 이미지를 빌드하고 배포하려면 다음 명령어를 실행합니다:

Terminal window
sst deploy --stage production

여기서는 어떤 스테이지 이름을 사용해도 되지만, 프로덕션용으로 새로운 스테이지를 만드는 것이 좋습니다. 이 명령어는 Deno 앱이 Fargate 서비스로 배포된 URL을 제공합니다.

Terminal window
Complete
MyService: http://prod-MyServiceLoadBalanc-491430065.us-east-1.elb.amazonaws.com

콘솔 연결하기

다음 단계로 SST 콘솔을 설정하여 앱을 _git push로 배포_하고 로그를 확인할 수 있습니다.

SST 콘솔 자동 배포

무료 계정을 생성하고 AWS 계정에 연결할 수 있습니다.