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
    • Helm Chart
    • Isito-EnvoyFilter
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Vault
    • Longhorn – Storage
    • VictoriaMetrics
  • CI/CD
    • ArgoCD
    • ArgoWorkflows
    • Spinnaker
    • Jenkins
  • Coding
    • Terraform
      • GCP – Google Cloud
      • AWS – Amazon Web Service
    • Golang
    • Laravel
    • Jquery & JavaScript
    • Git
    • Selenium
  • Log & Monitor
    • Prometheus
    • Grafana
    • ELK
      • Kibana
      • Logstash
  • BareMetal
  • 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

  • 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] Create an 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

[Terraform] – Terraform Beginner – Lesson 9: Terraform with AWS – part 2 AWS - Amazon Web Service
[Terraform] – Terraform Beginner – Lesson 7: Terraform Modules AWS - Amazon Web Service
[Terraform] – Terraform Beginner – Lesson 8: Terraform Functions and Conditional Expressions AWS - Amazon Web Service
[Terraform] Infrastructure Automation With Terraform – Lesson 1: Setup AWS AWS - Amazon Web Service
[Terraform] – Terraform Beginner – Lesson 4: Remote State AWS - Amazon Web Service
[Terraform] Error: Error acquiring the state lock 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

  • [Argocd/Vault] Integrate Vault inside Argocd by the plugin July 1, 2022
  • [Vault] Using Service Acount of Kubernetes to login Vault system. June 28, 2022
  • Protected: My Assignment  June 24, 2022
  • [Spinnaker] Spinnaker writes too many logs – Reduce spinnaker log level June 22, 2022
  • [Jenkins] Jobs will be created automatically by Jenkins Job Builder June 20, 2022

Archives

  • 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
  • CI/CD
    • ArgoCD
    • ArgoWorkflows
    • Jenkins
    • Spinnaker
  • Coding
    • Git
    • Golang
    • Jquery & JavaScript
    • Laravel
    • Selenium
    • Terraform
      • AWS – Amazon Web Service
      • GCP – Google Cloud
  • Kubernetes & Container
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Docker
    • Helm Chart
    • Isito-EnvoyFilter
    • Kubernetes
      • Ingress
    • Longhorn – Storage
    • Vault
    • VictoriaMetrics
  • Log & Monitor
    • ELK
      • Kibana
      • Logstash
    • Grafana
    • Prometheus
  • Uncategorized
  • Admin

Copyright © 2022 NimTechnology.