Skip to content

NimTechnology

Trình bày các công nghệ CLOUD một cách dễ hiểu.

  • Kubernetes & Container
    • Docker
    • Kubernetes
      • Ingress
      • Pod
    • Helm Chart
    • OAuth2 Proxy
    • Isito-EnvoyFilter
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Vault
    • Longhorn – Storage
    • VictoriaMetrics
    • MetalLB
    • Kong Gateway
  • CI/CD
    • ArgoCD
    • ArgoWorkflows
    • Argo Events
    • Spinnaker
    • Jenkins
    • Harbor
    • TeamCity
    • Git
      • Bitbucket
  • Coding
    • DevSecOps
    • Terraform
      • GCP – Google Cloud
      • AWS – Amazon Web Service
      • Azure Cloud
    • Golang
    • Laravel
    • Python
    • Jquery & JavaScript
    • Selenium
  • Log, Monitor & Tracing
    • DataDog
    • Prometheus
    • Grafana
    • ELK
      • Kibana
      • Logstash
  • BareMetal
    • NextCloud
  • Toggle search form

[Terraform] – Terraform Beginner – Lesson 3: Terraform with AWS

Posted on May 2, 2022May 2, 2022 By nim No Comments on [Terraform] – Terraform Beginner – Lesson 3: Terraform with AWS

Contents

Toggle
  • 1) Demo Setup an AWS Account.
  • 2) Introduction to IAM
  • 3) Demo IAM
    • 3.1) Create Administrator User
    • 3.2) Create a Group And Users of this group.
    • 3.3) View Policy and Create Custom Policy.
    • 3.4) Create Role
  • 4) Programmatic Access
  • 5) AWS IAM with Terraform
  • 6) IAM Policies with Terraform
  • 7) Introduction to AWS S3
  • 8) S3 with Terraform
  • 9 Introduction to DynamoDB
  • 10) DynamoDB with Terraform

1) Demo Setup an AWS Account.

Để register and init setup account AWS thì bạn đọc bài viết này

[AWS] – Terraform Beginner – Lesson 1: Create a Free Account of AWS

2) Introduction to IAM

Để hiểu sơ sơ về IAM thì mình xin giới thiếu các bạn 1 video.

Tiếp đến là phần hướng dẫn của mình.

User mà bạn đăng ký ban đầu đó sẽ là root account.

Từ user root sẽ tạo ra 4 user IAM.

Ông Lucy có thể login vào Web console cũng như runnign được command line
Nếu bạn config IAM và cấp cho lucy user với Policy là AdministrtorAccess.
Bạn thấy Lucy có quền mọi action và lên mọi resource.
Đây là 1 bẳng liên kết.
Nếu bạn muốn cấp cho user đó quyền Administrator thì bạn sẽ gán policy name là AdministratorAccess
Nếu bạn cấu hình policy cho từng user thì đó chưa phải là cách tối ưu.
Bạn sẽ tạo group và add các policy vào group đó.
Sau đó chúng ta có user mới là sẽ add thêm vào group là ok.
KHi mà bạn muốn EC2 connect đến S3 thì cũng sẽ cần config IAM

Chúng ta cũng có thể custom các policy cho phù hợp với business.

3) Demo IAM

Tạo user IAM

3.1) Create Administrator User

Chúng ta tạo user lucy. user này có access key để chúng ta sử dụng terraform, có thế login web console.

Chỗ này chúng ta sẽ review lại user sẽ được tạo như thế nào.
Phần này thì nên download và lưu cẩn thận các thông tin của user
giở bạn kiểm tra lại thấy đã có User lucy
CHúng ta đi vào chi tiết của user và show policy dưới dang JSON để quan sát.
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "iam:ChangePassword"
            ],
            "Resource": [
                "arn:aws:iam::*:user/${aws:username}"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "iam:GetAccountPasswordPolicy"
            ],
            "Resource": "*"
        }
    ]
}
Chúng ta có thể add thêm policy cho User.
Mình đang muốn cấp quyền administrator cho lucy nên mình sẽ click như anh trên.
ok giờ đã add thêm policy cho user.
policy AdministratorAccess thì mạnh như root vậy.

3.2) Create a Group And Users of this group.

giờ mình create user và auto gen pass.
Ở đây mình sẽ tạo Group.
mình tạo group và cấp policy Full Access EC2.
Đã có Group rồi Next thôi.
add tag để kiểu nhận diện cho dễ.
Giờ chúng ta đã tạo User và Goup.
Giờ chúng ta show detail về Group.

Như hình trên chúng ta thấy được là User đang chưa các User Nào?

bạn có thể được Group này có các policy nào?
và có thể add thêm policy cho Group.
Mình add thêm cho Group này Full Access S3

3.3) View Policy and Create Custom Policy.

CHúng ta có thể view các policy
Cũng như biết được policy đó đã được map với ai đó hay group nào đó.
Giờ chúng ta tạo 1 Custom Policy.
Giờ chọn service.
Mình cho Policy action List và Read

Giờ chúng ta sẽ thầy nó list và read được cái gì?

Lăn xuống tiếp

Đặt tên là policy và Create Policy
Đã tạo được Policy custom.

tường tự như trên chúng ta sẽ tạo Policy Read and List S3

3.4) Create Role

Role bạn chỉ nhớ đơn giản dùng để grant permission từ Service A đến service B
ví dụ. Ec2 có thể list và read S3

Xong rồi next tiếp

4) Programmatic Access

Bạn có thể cài và gõ command AWS CLI trên máy tính để access vào aws
Bạn có thể cài ở bất cứ OS nào!

Mình sẽ cài lên ubuntu:
https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
Kiểm tra version ngon là ok.
Các bạn có thể lấy region ở đây.
aws configure
AWS Access Key ID [None]: AKIATU2QSHII6XXXXX
AWS Secret Access Key [None]: rVGnya6x6VoLFgXYOGs0D2Ir+MXXXXXX
Default region name [None]: us-west-2
Default output format [None]: json

Bạn có thể kiểm tra lại configure

Bạn cũng có thể –help để thấy các gợi ý ngay lập tức.

5) AWS IAM with Terraform

https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_user

Ngoài resource chúng ta còn cần cấu hình provider
https://registry.terraform.io/providers/hashicorp/aws/latest/docs#provider-configuration

provider "aws" {
  region     = "us-west-2"
  access_key = "my-access-key"
  secret_key = "my-secret-key"
}
provider "aws" {
  region     = "us-west-2"
  access_key = "AKIATU2QSHXXXXXX"
  secret_key = "rVGnya6x6VoLFgXYOGs0D2Ir+Mxxxxxx"
}

resource "aws_iam_user" "admin-user" {
  name = "nim-admin"

  tags = {
    Description = "Devops Team Leader"
  }
}
Mình đã chạy terraform apply
đã thành công!
Kiểm tra console web thì chúng ta đã tạo được user

Nhưng là để luôn access key và secret trên code thì hơi nguy hiểm?

Bạn có thể để trong credentials AWS.
provider "aws" {
  shared_config_files      = ["/Users/tf_user/.aws/conf"]
  shared_credentials_files = ["/Users/tf_user/.aws/creds"]
  profile                  = "customprofile"
}
Hoặc để tron envenvironment.

https://registry.terraform.io/providers/hashicorp/aws/latest/docs#environment-variables

provider "aws" {}
$ export AWS_ACCESS_KEY_ID="anaccesskey"
$ export AWS_SECRET_ACCESS_KEY="asecretkey"
$ export AWS_REGION="us-west-2"
$ terraform plan

6) IAM Policies with Terraform

resource "aws_iam_user" "admin-user" {
  name = "nim-admin"

  tags = {
    Description = "Devops Team Leader"
  }
}

resource "aws_iam_policy" "adminUser" {
  name = "AdminUsers"
  policy = <<EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": "*",
      "Effect": "Allow",
      "Resource": "*"
    }
  ]
}
EOF
}

resource "aws_iam_user_policy_attachment" "nim-admin-access" {
  user       = aws_iam_user.admin-user.name
  policy_arn = aws_iam_policy.adminUser.arn
}
Sau đó apply ăn ngay.
Kiểm tra thì đã tạo user như yêu cầu
Đây là cách để json policy sang 1 file khác.

7) Introduction to AWS S3

S3 thì bạn sẽ hiều nó object storage. S3 dùng chưa file và thư mục như film, ảnh, file docx,…
S3 sẽ không thể làm nơi chạy hệ điều hành, không làm ổ boot, hay ổ system.

Cách hiện thị thư mục hay folder của S3
Đây phần policy của S3

8) S3 with Terraform

Chúng ta tạo bucket có tên “finanace-21092020”
Và push 1 file lên bucket.

https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket

resource "aws_s3_bucket" "finance" {
  bucket = "finance-20220502"

  tags = {
    Description        = "Finance and Payroll"
  }
}

resource "aws_s3_bucket_object" "finance-2022" {
  source = "nimtechnology.txt"
  bucket = aws_s3_bucket.finance.id
  key    = "nimtechnology-s3.txt"
}

Khi terraform plan:

Nhận được 1 warning:

Warning: Argument is deprecated

Vậy thay thế aws_s3_bucket_object bằng aws_s3_object

resource "aws_s3_bucket" "finance" {
  bucket = "finance-20220502"

  tags = {
    Description = "Finance and Payroll"
  }
}

resource "aws_s3_object" "finance-2022" {
  source = "nimtechnology.txt"
  bucket = aws_s3_bucket.finance.id
  key    = "nimtechnology-s3.txt"
}
Sau khi apply và kiểm tra trên console

Phần này ta config để 1 group trong IAM có thể access và action trên bucket S3

9 Introduction to DynamoDB

GIới thiệu về DynamoDB
Data của nó kiểu Key / Value
Chúng ta đã tạo thành công!
Bạn điện số vào member_id
xong chúng tạo thêm các data như name, age,…
OK giờ chúng ta kiểm tra lại item mà chúng ta vừa insert
Bạn cũng có thể filter data.
delete liền cho đỡ tốn tiền.

10) DynamoDB with Terraform

AWS - Amazon Web Service

Post navigation

Previous Post: [Terraform] – Terraform Beginner – Lesson 2: Working with Terraform.
Next Post: [GCP] How to delete payments, the visa card, the master cards come out of the Google Cloud flatform so easily

More Related Articles

[AWS] Creating AWS Certificate Manager (ACM) AWS - Amazon Web Service
[Aws] Access S3 or bucket from other account AWS AWS - Amazon Web Service
[csi-driver-smb] Providing PVC with type ReadWriteMany base on SMB AWS - Amazon Web Service
[AWS/EKS] EFS CSI Driver – Create Persistent Volume Clain with ReadWriteMany type on EKS AWS - Amazon Web Service
[S3] You will naturally see files on S3 being deleted AWS - Amazon Web Service
[AWS] Discovering how to design Cluster Autoscaler on EKS. AWS - Amazon Web Service

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tham Gia Group DevOps nhé!
Để Nim có nhiều động lực ra nhiều bài viết.
Để nhận được những thông báo mới nhất.

Recent Posts

  • [AWS/EKS] Cache Docker image to accelerate EKS container deployment. July 10, 2025
  • [Laravel] Laravel Helpful June 26, 2025
  • [VScode] Hướng dẫn điều chỉnh font cho terminal June 20, 2025
  • [WordPress] Hướng dấn gửi mail trên WordPress thông qua gmail. June 15, 2025
  • [Bitbucket] Git Clone/Pull/Push with Bitbucket through API Token. June 12, 2025

Archives

  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Categories

  • BareMetal
    • NextCloud
  • CI/CD
    • Argo Events
    • ArgoCD
    • ArgoWorkflows
    • Git
      • Bitbucket
    • Harbor
    • Jenkins
    • Spinnaker
    • TeamCity
  • Coding
    • DevSecOps
    • Golang
    • Jquery & JavaScript
    • Laravel
    • NextJS 14 & ReactJS & Type Script
    • Python
    • Selenium
    • Terraform
      • AWS – Amazon Web Service
      • Azure Cloud
      • GCP – Google Cloud
  • Kubernetes & Container
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Docker
    • Helm Chart
    • Isito-EnvoyFilter
    • Kong Gateway
    • Kubernetes
      • Ingress
      • Pod
    • Longhorn – Storage
    • MetalLB
    • OAuth2 Proxy
    • Vault
    • VictoriaMetrics
  • Log, Monitor & Tracing
    • DataDog
    • ELK
      • Kibana
      • Logstash
    • Fluent
    • Grafana
    • Prometheus
  • Uncategorized
  • Admin

Copyright © 2025 NimTechnology.