Implementing AWS SDK in NestJS

Dante Rigoli

Implementing AWS SDK in NestJS: A Practical Guide

At Phantom Agency, we strive to leverage the latest technologies to deliver cutting-edge solutions to our clients. In this blog post, we’ll explore the implementation of the AWS SDK in NestJS, a popular Node.js framework. The AWS SDK provides powerful tools for integrating various Amazon Web Services into your application, enabling seamless cloud functionality and scalability.


Before we dive into the implementation, make sure you have the following prerequisites in place:


To get started, let’s install the AWS SDK package via npm:

npm install aws-sdk


Next, we need to configure the AWS SDK with your AWS credentials and region. Create a new file called aws.config.ts and add the following code:

// aws.config.ts
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import AWS from 'aws-sdk';

export class AwsConfigService {
  private awsConfig: AWS.Config;

  constructor(private readonly configService: ConfigService) {
    this.awsConfig = new AWS.Config({
      region: configService.get<string>('AWS_REGION'),
      accessKeyId: configService.get<string>('AWS_ACCESS_KEY_ID'),
      secretAccessKey: configService.get<string>('AWS_SECRET_ACCESS_KEY'),

  get aws(): AWS.Config {
    return this.awsConfig;

Make sure you have your AWS credentials (access key ID and secret access key) and AWS region available. We recommend using environment variables to securely store and retrieve these values. In the code above, we use the ConfigService from NestJS to fetch the values from the environment.


Now that we have the AWS SDK configured, let’s explore some common use cases. In this example, we’ll focus on using the AWS SDK’s S3 service to upload a file.

// app.service.ts
import { Injectable } from '@nestjs/common';
import { S3 } from 'aws-sdk';

export class AppService {
  constructor(private readonly awsConfigService: AwsConfigService) {}

  async uploadFileToS3(file: Express.Multer.File): Promise<string> {
    const s3 = new S3(this.awsConfigService.aws);

    const uploadParams: S3.PutObjectRequest = {
      Bucket: 'your-bucket-name',
      Key: file.originalname,
      Body: file.buffer,

    const result = await s3.upload(uploadParams).promise();
    return result.Location;

In the code snippet above, we create a new instance of the S3 service using the AWS configuration from AwsConfigService. We then define the upload parameters, including the bucket name, file key, and file body. By calling s3.upload(uploadParams).promise(), we upload the file to the specified S3 bucket and retrieve the resulting URL.


Congratulations! You have successfully implemented the AWS SDK in your NestJS application. This opens up a world of possibilities for leveraging various AWS services, such as S3, DynamoDB, and more. Remember to explore the AWS SDK documentation for a comprehensive understanding of the available services and their APIs.

At Phantom Agency, we continue to explore new technologies and share our knowledge to empower businesses with innovative solutions. Stay tuned for more insightful blog posts and tutorials.

If you have any questions or need assistance with your AWS integration, feel free to reach out to our expert team. Happy coding!

[AWS]: Amazon Web Services [SDK]: Software Development Kit