• Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Search

Archives for June 2023

Python Read File Multiple Columns

June 26, 2023

Here’s how to read a file line by line with multiple columns.

with open('file.txt') as file:
    for line in file:
        line = line.strip()
        columns = line.split()
        col1 = columns[0]
        col2 = columns[1]
        some_function(col1, col2)

with open('file.txt') as file: for line in file: line = line.strip() columns = line.split() col1 = columns[0] col2 = columns[1] some_function(col1, col2)

Filed Under: Misc Tagged With: columns, file, line, python, read

Gsed

June 22, 2023

The sed utility on a Mac OS running Z shell or zsh is a little different compared to gnu’s sed.

Install gnu sed on Mac OS.

brew install gnu-sed

brew install gnu-sed

Use gsed instead of sed.

gsed --version

gsed --version

Filed Under: Linux Tagged With: gsed, mac, os, sed, zsh

AWS Terraform Create Policy and Attach

June 21, 2023

Here’s the Terraform code that creates an AWS IAM policy and attaches it to an existing role (ROLENAME).

# CREDENTIALS
provider "aws" {
  shared_credentials_files = ["~/.aws/credentials"]
  shared_config_files      = ["~/.aws/config"]
  profile                  = "default"
  region                   = "us-east-1"
}
# CREATE POLICY
resource "aws_iam_policy" "s3-policy" {
    name        = "s3-policy"
    description = "Allow role to write to bucket"
    policy = "${file("policy.json")}"
}
# ATTACH POLICY TO ROLE
resource "aws_iam_role_policy_attachment" "s3-policy-attachment" {
  role       = "ROLENAME"
  policy_arn = aws_iam_policy.s3-policy.arn
}

# CREDENTIALS provider "aws" { shared_credentials_files = ["~/.aws/credentials"] shared_config_files = ["~/.aws/config"] profile = "default" region = "us-east-1" } # CREATE POLICY resource "aws_iam_policy" "s3-policy" { name = "s3-policy" description = "Allow role to write to bucket" policy = "${file("policy.json")}" } # ATTACH POLICY TO ROLE resource "aws_iam_role_policy_attachment" "s3-policy-attachment" { role = "ROLENAME" policy_arn = aws_iam_policy.s3-policy.arn }

Here’s the policy.json file.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "s3:PutObject",
                "s3:PutObjectAcl",
                "s3:ListBucket",
                "s3:GetBucketLocation",
                "s3:GetObject",
                "s3:GetObjectAcl",
                "s3:DeleteObject"
             ],
            "Resource":"arn:aws:s3:::your-bucket/*"
            }
        }
    ]
}

{ "Version": "2012-10-17", "Statement": [ { "Sid": "VisualEditor0", "Effect": "Allow", "Action": [ "s3:PutObject", "s3:PutObjectAcl", "s3:ListBucket", "s3:GetBucketLocation", "s3:GetObject", "s3:GetObjectAcl", "s3:DeleteObject" ], "Resource":"arn:aws:s3:::your-bucket/*" } } ] }

Filed Under: Cloud Tagged With: aws, policies, roles, terraform

  • Go to page 1
  • Go to page 2
  • Go to page 3
  • Go to page 4
  • Go to Next Page »
  • Home
  • About
  • Search

Copyright © 2023