Boost your AWS skills

A helpful guide for new AWS developers & enthusiasts

By
  • Omar Sherif
Apr. 26 20234 min. read time
aws-services3.jpeg

AWS Services

If you are new to AWS you are probably interested to try its popular services as AWS Lambda, Amazon S3, Amazon DynamoDB, AWS ECS, AWS Fargate (Containers) and the list goes on...

But like me, you can also be overwhelmed with the number of services that AWS has to offer which has reached +200 service for 2023

Your journey as a developer to AWS

In your journey to become an AWS developer you may have started reading some of the AWS Docs or took a course that will teach you how to create an AWS Lambda function or configure an Amazon S3 database using either the console or the CLI. This can be useful and fast when you are developing small applications or trying out AWS services

But in reality, your production app might be more sophisticated than that, you can have an application that consists of: 10 AWS lambdas, 2 Amazon S3 Buckets, 3 Amazon API Gateways, 2 AWS EC2 Servers, ...

Jaeger waterfall diagram for gatsby's build command

So at some point when you want to deploy your application into production, you will probably come across some interesting questions as:

  • How to easily manage and have a consistent architecture across different regions and accounts ?
  • How to make your app fault tolerant if someone accidently deleted or changed some settings ?
  • How can you backup your configurations and restore it when needed ?
  • How to quickly add/remove resources ?

To answer these questions and to help you as an AWS Developer easily provision your AWS resources in production, you should know more about AWS Cloudformation

AWS CloudFormation (Iaas)

AWS CloudFormation is a service that allows you to define and manage AWS resources which are called Stacks by writing documents primarily in YAML or JSON formats which are known as Templates

With CloudFormation, you can create, update, and delete AWS resources as a single, coordinated operation, and manage infrastructure resources in a safe, repeatable manner (IaaS)

While AWS CloudFormation itself isn't a new service and was launched in 2011, but knowing about this service is essential to boost your skills from just developing AWS resources ==> to manage, develop and have a robust way to control your AWS resources

Jaeger waterfall diagram for gatsby's build command

AWS CloudFormation

AWS CloudFormation nice features

  • Writing templates isn't limited to JSON/YAML only, you can write with programming languages as (TypeScript, Python, Java, and .NET. AWS)
  • Safety when updating your Stacks by providing you the ability to preview your changes before deploying and supporting automatic rollback in case of failing to update
  • Be creative with your Stacks with the Nesting stacks feature, meaning you can create small stacks and use them as building blocks for more complex Stacks
  • Extensible and supports 3rd party partners (MongoDB, Datadog, Atlassian, etc..)

Use case and how will your template look like

Imagine you have a use case where it was required from you to develop an AWS lambda that should be able to connect to a DynamoDB Table and an S3 Bucket

How can you write a CloudFormation template to define your resources ? Let's have a look on this template that is defined in Typescript

import * as cdk from 'aws-cdk-lib';
import { Stack, StackProps } from 'aws-cdk-lib';
import * as s3 from 'aws-cdk-lib/aws-s3';
import * as dynamodb from 'aws-cdk-lib/aws-dynamodb';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as iam from 'aws-cdk-lib/aws-iam';

export class ThreeResourcesStack extends Stack {
  constructor(scope: cdk.Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    const s3Bucket = new s3.Bucket(this, 'S3Bucket');

    const dynamoDBTable = new dynamodb.Table(this, 'DynamoDBTable', {
      partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING },
      billingMode: dynamodb.BillingMode.PAY_PER_REQUEST
    });

    const lambdaFunctionRole = new iam.Role(this, 'LambdaFunctionRole', {
      assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com')
    });

    const s3AccessPolicy = new iam.PolicyStatement({
      effect: iam.Effect.ALLOW,
      actions: [
        's3:ListBucket',
        's3:GetObject',
        's3:PutObject'
      ],
      resources: [
        s3Bucket.bucketArn,
        `${s3Bucket.bucketArn}/*`
      ]
    });

    lambdaFunctionRole.addToPolicy(s3AccessPolicy);

    const lambdaFunction = new lambda.Function(this, 'LambdaFunction', {
      runtime: lambda.Runtime.NODEJS_14_X,
      code: lambda.Code.fromAsset('lambda'),
      handler: 'index.handler',
      role: lambdaFunctionRole,
      environment: {
        BUCKET_NAME: s3Bucket.bucketName,
        TABLE_NAME: dynamoDBTable.tableName
      }
    });

    dynamoDBTable.grantReadWriteData(lambdaFunction);
  }
}

Code breakdown

With the help of the AWS CDK Lib, we are able to write a template in TypeScript that creates:

  • a DynamoDB table & define it's index and billing methods
  • S3 Bucket
  • a lambda that is connected to both DyanmoDB and S3 with the required permissions and IAM Roles.

All in one template without the need to create each resource individually in a predictable and controlled fashion with the AWS CloudFormation magic 🪄

Summary

Being able to create AWS resources and knowing which resource to use for your apps, is one of the first steps for you as an AWS Developer. However if you want to take your application to the next step to be ready for production, you should start thinking on how to manage your resources by using services as AWS CloudFormation

  • AWS
  • Cloud
  • Infrastructure
Disclaimer: The views and opinions expressed in this article are those of the author and do not necessarily reflect the official policy or position of DNB.

© DNB

To dnb.no

Informasjonskapsler

DNB samler inn og analyserer data om din brukeratferd på våre nettsider.