Implementing Infrastructure as Code with Terraform | AWS Cloud Resume Challenge - Part 6

Rishab in Cloud
26 Apr 202313:48

Summary

TLDRIn part six of the AWS Cloud resume challenge, host Richard dives into the concept of Infrastructure as Code (IaC) with Terraform, chosen by the audience. He guides through setting up a provider configuration, creating a Lambda function, and deploying it with Terraform. Richard also covers deploying a Lambda function URL and addressing IAM permissions for accessing DynamoDB. The video concludes with a functional Lambda setup, a walkthrough of Terraform files, and suggestions for further automation with GitHub actions, inviting viewers to explore creating IaC for DynamoDB, S3, and CloudFront.

Takeaways

  • 😀 The video is part of a series on the AWS Cloud resume challenge, hosted by Richard.
  • 🛠️ The series has covered various topics including CI/CD setup for an S3 bucket, creation of an S3 bucket with CloudFront, a DynamoDB table, and a Lambda function in Python.
  • 📈 The main focus of this part is on IaC (Infrastructure as Code), with an emphasis on using Terraform over CloudFormation based on audience poll results.
  • 📝 The tutorial guides through setting up the `provider.tf` file for Terraform, detailing the use of AWS CLI profiles or direct access key and secret key setup.
  • 🔧 It demonstrates creating a Lambda function and an IAM rule using Terraform, including the use of a zip file for the Lambda function code deployment.
  • 📁 The script mentions creating a `func.py` file containing the Python code for the Lambda function, which is then referenced in the Terraform files.
  • 🚀 The process of deploying infrastructure with Terraform is explained, including the use of `terraform init`, `terraform plan`, and `terraform apply` commands.
  • 🔗 The importance of the state file in Terraform for tracking infrastructure changes is highlighted.
  • 🔒 A section is dedicated to adding a Lambda function URL with proper authorization settings to allow public access or restrict it to specific domains.
  • 🛡️ The video addresses a common error related to IAM permissions, showing how to attach a policy to an IAM role to grant the Lambda function access to DynamoDB.
  • 🔄 The script suggests automating further deployments with GitHub Actions and setting up a CI/CD pipeline for backend changes and testing.

Q & A

  • What is the main topic covered in part six of the AWS Cloud resume challenge?

    -The main topic covered is Infrastructure as Code (IC), specifically using Terraform for deploying AWS resources.

  • What AWS services were set up in the previous parts of the challenge?

    -In the previous parts, they set up CI/CD for an S3 bucket, created an S3 bucket with CloudFront to host a website, and created a DynamoDB table and a Lambda function in Python.

  • Why did the audience choose Terraform over CloudFormation?

    -The audience preferred Terraform based on a poll conducted by the host, although the exact reasons for their preference are not specified in the script.

  • What is the purpose of the Lambda function created in the script?

    -The Lambda function is used to get a viewer count from a DynamoDB table and display it on the website.

  • How is the Lambda function code transferred to the AWS environment using Terraform?

    -The Lambda function code is transferred using a zip file that contains the Python code written for the Lambda function.

  • What is the significance of the 'terraform plan' command?

    -The 'terraform plan' command shows what changes are planned to be made to the AWS infrastructure, such as adding or modifying resources.

  • Why was an 'Internal Server Error' encountered when trying to access the Lambda function URL?

    -The error occurred because the Lambda function's IAM role did not have the necessary permissions to access DynamoDB, which it requires to get and update the viewer count.

  • What is the role of the 'terraform apply' command in the deployment process?

    -The 'terraform apply' command is used to create or update resources in the AWS infrastructure according to the Terraform configuration files.

  • What is the purpose of the 'terraform state' file?

    -The 'terraform state' file keeps track of the current state of the infrastructure, allowing Terraform to understand what has changed and what needs to be updated or created.

  • How can the deployment process be automated using GitHub Actions?

    -GitHub Actions can be set up to automatically deploy changes made to the Terraform files, ensuring that updates to the infrastructure are applied quickly and consistently.

  • What additional resources could be managed by Terraform as suggested in the script?

    -The script suggests creating Terraform files for managing a DynamoDB table, S3 bucket, and CloudFront distribution, in addition to the Lambda function.

Outlines

00:00

🚀 Introduction to Terraform in AWS Cloud Resume Challenge

In the sixth part of the AWS Cloud Resume Challenge, host Richard reviews the series' progress, which includes setting up CI/CD for an S3 bucket, creating a website-hosting S3 bucket with CloudFront, and establishing a DynamoDB table and a Python Lambda function to display viewer counts. Richard introduces the main topic of this segment, Infrastructure as Code (IaC), and decides to use Terraform based on audience feedback. He explains the process of setting up the 'provider.tf' file for Terraform, detailing the use of AWS CLI profiles or manual access key and secret key entry. The main infrastructure code, including the Lambda function and its related resources, is to be written in the 'main.tf' file.

05:00

🛠 Deploying Lambda Function with Terraform

Richard demonstrates deploying a Lambda function using Terraform by creating a 'my_func' Lambda function and an IAM rule. He explains the process of uploading the Lambda function code through a zip file and setting up the necessary directory and 'func.py' file with the Python code for the Lambda function. Richard then shows the deployment process using 'terraform init', 'terraform plan', and 'terraform apply', highlighting the importance of the state file in tracking infrastructure changes. After deploying the Lambda function, he discusses the need to configure a function URL for triggering the Lambda function and updates the Terraform files accordingly.

10:03

🔒 Fixing Permissions and Finalizing Lambda Function Deployment

After deploying the Lambda function, Richard discovers an 'Internal Server Error' due to missing permissions for the Lambda function to access DynamoDB. He rectifies this by creating a new IAM policy that grants the necessary 'update-item' and 'get-item' actions for the DynamoDB table. Richard then attaches this policy to the IAM role associated with the Lambda function. After making these changes and running 'terraform plan' and 'terraform apply' again, the Lambda function is successfully updated with the correct permissions, and the function URL starts returning the correct viewer count from the DynamoDB table.

🔄 Automating Deployments with GitHub Actions and Future Plans

Richard concludes the video by discussing the automation of deployments using GitHub Actions for any changes made to the Terraform files. He suggests setting up a '.gitignore' file to exclude sensitive credentials from commits. Richard also invites viewers to create additional Terraform files for other resources like DynamoDB tables and S3 buckets, and to consider using Pulumi or CloudFormation for IaC. He hints at future content involving CI/CD setup for the backend, including GitHub Actions for deployment and testing of the Python code. Richard encourages viewers to like, subscribe, and comment on their experience with the series.

Mindmap

Keywords

💡AWS Cloud

AWS Cloud, short for Amazon Web Services Cloud, is a comprehensive and widely adopted cloud platform that offers a variety of services, including computing, storage, databases, and more. In the video, AWS Cloud is the main platform where the host, Richard, is demonstrating the setup and deployment of various cloud resources as part of the 'resume challenge' series.

💡CI/CD

CI/CD stands for Continuous Integration/Continuous Deployment, a set of practices in software development designed to improve the process of integrating changes from multiple contributors into a single software project. In the script, CI/CD is mentioned in the context of setting up for an S3 bucket, which is part of the front-end infrastructure being automated.

💡S3

S3, or Amazon Simple Storage Service, is an object storage service offered by AWS that provides developers and IT teams with secure, scalable, and highly available storage. The script discusses the creation of an S3 bucket to host a website, which is a common use case for S3 in web development.

💡CloudFront

CloudFront is a content delivery network (CDN) service provided by AWS. It accelerates the distribution of static and dynamic web content, such as .html, .css, .js, and image files, to users around the world. In the video, CloudFront is used in conjunction with the S3 bucket to improve the website's performance.

💡DynamoDB

DynamoDB is a fully managed NoSQL database service provided by AWS that offers fast and predictable performance with seamless scalability. In the script, a DynamoDB table is created to store and retrieve data, such as a visitor count for the website, which is a typical application of DynamoDB in web applications.

💡Lambda

AWS Lambda is a serverless compute service that lets you run code without provisioning or managing servers. In the video, a Lambda function written in Python is created to get a visitor count from the DynamoDB table and display it on the website, showcasing the serverless architecture capability of AWS.

💡Infrastructure as Code (IaC)

Infrastructure as Code (IaC) is a DevOps practice where infrastructure is described and managed using code, allowing for version control and automation. The script introduces IaC as a main concept of DevOps and discusses using Terraform, a popular IaC tool, for managing AWS resources.

💡Terraform

Terraform is an IaC tool developed by HashiCorp that enables users to build, change, and version infrastructure safely and efficiently. In the video, Terraform is chosen over CloudFormation based on a poll and is used to script the deployment of AWS resources such as the Lambda function.

💡IAM

IAM stands for AWS Identity and Access Management, a service that helps you securely control access to AWS resources. The script mentions setting up an IAM user with the necessary permissions to deploy resources, which is a critical step in managing access to AWS services.

💡Lambda Function URL

A Lambda Function URL is a feature that allows direct invocation of a Lambda function via an HTTP endpoint. In the script, the host configures a Lambda Function URL to trigger the Lambda function that retrieves the viewer count, demonstrating a practical use of this feature for serverless applications.

💡GitHub Action

GitHub Action is a feature of GitHub that allows you to automate workflows, such as software development processes including build, test, and deployment. The script suggests setting up a GitHub Action for further deployments of the Terraform configurations, which would automate the process of updating the infrastructure when changes are made.

Highlights

Introduction to part six of the AWS Cloud resume challenge series by host Richard.

Overview of previous accomplishments in setting up CI/CD for S3, creating an S3 bucket with CloudFront, and developing a DynamoDB table and Lambda function.

Introduction of the main concept of DevOps: Infrastructure as Code (IC).

Decision to use Terraform over CloudFormation based on audience poll results.

Explanation of setting up the AWS provider with either a profile or access keys in the provider.tf file.

Diving into writing Terraform code, starting with the Lambda function configuration.

Mention of AWS Lambda function URLs as a new resource in the AWS Terraform provider.

Instructions on creating a Lambda function and IAM rule using a zip file for code deployment.

Details on structuring the Lambda directory and func.py file for the Python code.

Demonstration of deploying infrastructure with Terraform and the importance of the state file.

Use of Terraform commands 'plan' and 'apply' to deploy AWS resources.

Verification of Lambda function deployment and the addition of a function URL for triggering.

Encountering an internal server error due to missing permissions for the Lambda function to access DynamoDB.

Correction of the IAM policy to include necessary permissions for DynamoDB access.

Attachment of the corrected IAM policy to the IAM role for the Lambda function.

Final deployment using Terraform to fix the internal server error and successfully retrieve viewer count.

Summary of building Terraform files for deploying a Lambda function directly from a code editor.

Suggestion to set up GitHub actions for automated deployments and include testing for the Python code.

Invitation for viewers to provide feedback and suggestions for future parts of the series.

Transcripts

play00:00

welcome to part six of the AWS Cloud

play00:03

resume challenge this is your host

play00:04

Richard and you know I've been doing

play00:06

this challenge for a while now but we

play00:09

have covered a lot of topics in the

play00:11

previous five parts so we have set up

play00:14

cicd for our S3 which is the front end

play00:17

we created an S3 bucket with cloudfront

play00:20

which hosts our website we created a

play00:22

dynamodb table and also a Lambda

play00:25

function in Python that gets a severe

play00:28

count from our dynamodb table and

play00:30

displays it on our website so I hope

play00:32

you're enjoying this series today I'm

play00:34

gonna cover some really interesting

play00:36

topic which is one of the main concepts

play00:38

of devops known as IC which stands for

play00:41

infrastructure as code and as I did a

play00:44

poll few weeks ago on what should I

play00:46

choose as the tool you know terraform or

play00:49

cloud formation a lot of you said

play00:50

terraform so we'll be trying out some

play00:53

terraform today

play00:54

coming up to the cloud resume challenge

play00:57

website so this is the part that we'll

play01:00

be covering which means we are almost

play01:03

done with this challenge you should have

play01:05

a working project by now with that being

play01:08

said let's Dive Right into writing some

play01:11

terraform

play01:21

so in your provider.tf

play01:24

you can either have the profile so if

play01:27

you have set up AWS CLI with profiles

play01:30

you can list that profile here if you

play01:33

don't know I'll leave it linked down

play01:34

below and you can read more about how to

play01:36

set up adobe CLI profile if not you will

play01:40

have to set up the access key and the

play01:42

secret key for your IM user that has

play01:44

access to deploy the resources that

play01:46

we'll be deploying today so it's either

play01:49

this or your profile

play01:51

so now the main.tf comes into play so

play01:56

this is where all of the main

play01:58

infrastructure code will go so in our

play02:00

case I'm going to start with the Lambda

play02:03

function and I also would you like to

play02:06

remind that we used Lambda function URLs

play02:09

which is now an available resource in

play02:13

the AWS terraform provider but before we

play02:16

can get started with the URL we first

play02:19

need a Lambda function so let's create a

play02:22

Lambda function called my function or

play02:25

I'll make it short for my func

play02:30

and now we'll also create a im rule for

play02:34

our Lambda function

play02:36

and the way we upload our Lambda

play02:39

function through terraform is by using a

play02:42

zip file so I'll add the code to do that

play02:45

here now

play02:46

so now what we have to do as you can see

play02:49

as per the instructions that I mentioned

play02:51

here I need a Lambda directory and

play02:55

inside that Lambda directory we'll be

play02:57

using a file called func Dot py

play03:04

and this func.py will basically contain

play03:08

our python code that we wrote in the

play03:11

console for a Lambda function so just to

play03:14

remind you this is how our Lambda

play03:16

function looked like in the AWS console

play03:18

what I'm going to do is just copy this

play03:20

python code and paste it in our func.py

play03:24

file here and don't get confused by the

play03:27

names you can name whatever you want

play03:29

your Lambda function to be which in my

play03:32

case is my func or even the func.py file

play03:36

just make sure to reference them in our

play03:39

terraform files accordingly so I'm just

play03:41

gonna do Ctrl s and coming back to the

play03:45

main.tf everything looks good so now let

play03:48

me show you how it would look like if it

play03:50

did deploy this infrastructure using

play03:53

terraform so what I'm going to do is

play03:55

going to provider.tf since I don't want

play03:57

to set up access key or secret key I do

play04:00

have a profile set up on my local

play04:02

machine here which is called default I

play04:05

believe so if I click controller s and

play04:07

open up my terminal here we have to go

play04:10

into the infra directory right and now

play04:13

here we will do a terraform in it which

play04:16

will initialize the terraform directory

play04:20

that we have so it launches so it

play04:22

creates a bunch of files like terraform

play04:26

lock and also the state files which

play04:28

remembers what the state of our

play04:31

infrastructure is and you will

play04:33

understand more as I go through this

play04:35

demo here so let's do terraform plan

play04:38

which will show us what do we plan on

play04:42

deploying to our AWS infrastructure and

play04:45

as you can see plan says two to add one

play04:48

of them being the Lambda function and

play04:51

then the I am rule that we have so let's

play04:55

do

play04:56

terraform apply enter yes

play05:00

it'll start creating the Lambda function

play05:03

for us so we'll give it a minute to do

play05:06

that

play05:08

okay so the apply has been completed and

play05:10

you can see two resources were added as

play05:13

per our terminal the region was a

play05:16

central so let's go look at our AWS

play05:18

console to see if the Lambda function

play05:21

was deployed so you can see my func was

play05:24

deployed 34 seconds ago and if you click

play05:27

on that you will see that the code has

play05:30

been pushed right as we wanted it to be

play05:33

the only thing is it won't have a

play05:37

function URL because we didn't configure

play05:39

it in our terraform file so let's go

play05:42

back and do that because we needed the

play05:45

URL to trigger our Lambda function right

play05:48

because that's how we get the viewer

play05:50

count from dynamodb okay so coming back

play05:53

to our main.tf we need to add a chunk of

play05:57

code here to deploy function URL and the

play06:01

way you do that is by using a resource

play06:04

called AWS underscore Lambda underscore

play06:06

URL

play06:08

so this is the resource remember I have

play06:11

set the authorization Type To None So

play06:14

that the URL is publicly accessible but

play06:17

you can set up calls so that the

play06:20

function can only be called from your

play06:22

own resume website so in allow regions

play06:25

make sure you have your own website's

play06:28

domain so I'll do Ctrl s which would

play06:31

save domain.tf and open the terminal

play06:34

again here and let's do terraform plan

play06:37

again this is where these State Files

play06:40

come into play so as you can see it

play06:42

plans on adding one other resource now

play06:44

which is our Lambda function URL so this

play06:48

is how it identifies that oh if I'm

play06:51

changing the infrastructure because

play06:53

remember our main.tf has all three

play06:55

resources right the Lambda function the

play06:57

IM Rule and the Lambda function URL but

play07:01

instead it only said want to add because

play07:03

it checked in the state file that the

play07:06

other two resources are already deployed

play07:09

so I hope you understand the concept of

play07:12

the state file here

play07:13

so we'll do terraform apply and like

play07:16

last time we'll enter yes

play07:18

and there we go the apply was complete

play07:21

let's go back to our Lambda function

play07:23

here and let's do a quick refresh we'll

play07:27

see that it was the last modified 15

play07:29

seconds ago and if you look at

play07:31

configuration you can see we have a

play07:33

function URL

play07:35

so if we click on that I want you to

play07:38

guess in the comments what would happen

play07:39

I think that we should get a viewer

play07:41

count but we forgot one crucial step

play07:45

internal server error so the reason we

play07:48

get an internal server error is because

play07:50

the Lambda function that we set up it

play07:53

has an IEM rule remember in our main.tf

play07:57

and in that policy statement we didn't

play08:00

include dynamodbe and our Lambda

play08:04

function does need access to dynamodb in

play08:07

order to get and update the account so

play08:10

let's change this policy okay so let's

play08:13

create another IM role policy that we

play08:17

will attach to our IAM rule so coming

play08:21

down here and I'm just also going to

play08:24

link the AWS documentation that you will

play08:28

find handy when you're creating this

play08:30

policy so I'm just going to quickly type

play08:32

it down

play08:35

okay so I have my I am policy here as

play08:39

you can see AWS I'm policy and I've just

play08:42

named it I am policy for a resume

play08:44

project and you can change this right

play08:46

and if you look at the policy it is

play08:49

allowing the action here called update

play08:51

item and get item for dynamodb

play08:54

and let's make sure the intent is nice

play08:57

the resource make sure you update this

play09:00

to your table name so in my case the

play09:02

table name is resume Dash challenge as

play09:05

you can see in the python function here

play09:07

so whatever your dynamodb table name is

play09:10

make sure it's you know pointing to that

play09:12

if I click Ctrl s that will save our

play09:15

main.tf file

play09:16

and now the last thing to do is attach

play09:20

this policy to our IM rule which we can

play09:23

do here at the bottom so if I do

play09:27

resource AWS IM role policy attachment

play09:30

so you can find more information on this

play09:33

resource on the AWS provider for

play09:35

terraform and I am attaching the rule

play09:39

that we created called IAM for Lambda to

play09:43

the policy that we created called IM

play09:44

policy for a resume project so let's do

play09:47

that

play09:51

and then the policy is

play09:55

there we go so we have our that resource

play09:59

ready so if we do Ctrl s and open my

play10:02

terminal here

play10:04

let's do terraform plan

play10:07

so we have some syntax error on line 61

play10:11

which is right here so I forgot

play10:15

to put dot name at the end so we'll do a

play10:18

control s again and do terraform plan

play10:21

again to see if everything looks good so

play10:24

yes there are two changes or two things

play10:27

that need to be added so we'll do

play10:28

terraform apply and I'll ask for a yes

play10:33

or no we'll go with Yes again and there

play10:35

we go two resources were added so now if

play10:39

I go to my functions here click on my

play10:42

func and we know that it already had the

play10:45

role name as I am for Lambda as we

play10:48

described in our main.tf so if we look

play10:50

at the permission you can see that there

play10:52

is a policy attached now right and if I

play10:55

open that up and you can see it has

play10:57

dynamodb read and write the table name

play11:00

is resume Dash challenge

play11:02

so now if we click on this URL and I

play11:06

hope we get the viewer account and you

play11:08

can see we are getting 29388 which is my

play11:13

Tano DB tables value right now so we

play11:16

finally have our function working again

play11:19

in this video we build terraform files

play11:22

to deploy Lambda function so this is the

play11:26

python function that got deployed right

play11:29

from our code editor and we can set up

play11:32

GitHub action for you know further

play11:34

deployments of this terraform so let's

play11:38

say we change you know something in our

play11:40

policy or if we go to Lambda and we

play11:43

changed our function dot pi to do

play11:46

something different we can have a GitHub

play11:48

action workflow that will deploy the

play11:50

changes automatically

play11:52

so just to wrap it up we have main.tf we

play11:55

have provider.tf and we have function.pi

play11:58

which is our Lambda function in Python

play12:01

and all of this is available under the

play12:04

infra directory on my GitHub repo so you

play12:07

can check it out I would also recommend

play12:09

adding a git dot ignore to your info

play12:12

folder to make sure you don't commit you

play12:15

know your credentials and stuff so in

play12:17

order to take this challenge folder what

play12:19

you can do is create write terraform

play12:21

code for creating dynamodb table

play12:24

terraform code for your S3 bucket so

play12:27

like all of the infrastructure bits that

play12:29

we created you know dynamodb Lambda and

play12:32

S3 are created by terraform and you can

play12:35

use palumi or cloud formation to do your

play12:38

ioc so for the next part comment down

play12:42

below but what I was thinking is have

play12:44

the CI CD pod set up for the backend

play12:46

which means the GitHub action that I was

play12:48

talking about which would deploy any of

play12:50

the changes we do to our main main.tf or

play12:53

any of the terraform files and also

play12:55

include some testing so I remember we

play12:59

left out testing so maybe we set up some

play13:01

tests for our python code so you know

play13:03

the Lambda function and we do some mock

play13:06

tests for how the dynamodb gets called

play13:09

and like some mock responses that we can

play13:11

do so let me know if you're interested

play13:13

in that but I feel pretty good about

play13:15

this Challenge and as I said you can

play13:17

take it a step further by creating

play13:19

terraform files for your dynamodb and S3

play13:21

and cloudfront I hope you're liking this

play13:24

series if you are please make sure you

play13:26

like and are subscribed to the channel

play13:28

to stay up to date I have some more

play13:30

projects coming down the line and make

play13:32

sure you comment down if you found any

play13:35

issues because there are always some

play13:37

weird I am issues that I leave out and

play13:40

then I try to cover them in the next

play13:42

video so yeah I'll see you in the next

play13:44

one peace

Rate This

5.0 / 5 (0 votes)

Related Tags
DevOpsAWS CloudTerraformCI/CDS3 BucketCloudFrontDynamoDBLambda FunctionInfrastructure as CodeTutorial SeriesCode Deployment