• Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Archives

Archives for December 2018

Terraform

December 30, 2018

Hashicorp has a product called Terraform, which is a provisioning tool for the cloud. It works with most major providers like AWS, GCP, Azure and many more. Unlike CloudFormation, Resource Manager and Deployment Manager, Terraform will work with not just its own, but with many cloud providers. Here’s an example of how to provision a single instance to AWS, and assign an elastic IP to it. The cloud resources in the example were redacted. Placeholder values were placed instead for security purposes.

provider "aws" {
	access_key = "xxxxxxxx"
	secret_key = "xxxxxxxxxxxxxxxxxxxxxxxxxx"
	region = "us-east-2"
}
 
resource "aws_instance" "server1" {
	ami = "ami-xxxxxxxxxxxxxx"
	key_name = "tfc-ohio"
	instance_type = "c5.2xlarge"
	subnet_id = "subnet-xxxxxxxx"
	security_groups	= ["sg-xxxxxxxxxxxxxxx"]
	private_ip = "10.0.4.100"
	tags {
		Name = "Server One"
	}
}
 
resource "aws_eip_association" "server1-eip" {
  instance_id = "${aws_instance.server1.id}"
  allocation_id = "eipalloc-xxxxxxxx"
}

provider "aws" { access_key = "xxxxxxxx" secret_key = "xxxxxxxxxxxxxxxxxxxxxxxxxx" region = "us-east-2" } resource "aws_instance" "server1" { ami = "ami-xxxxxxxxxxxxxx" key_name = "tfc-ohio" instance_type = "c5.2xlarge" subnet_id = "subnet-xxxxxxxx" security_groups = ["sg-xxxxxxxxxxxxxxx"] private_ip = "10.0.4.100" tags { Name = "Server One" } } resource "aws_eip_association" "server1-eip" { instance_id = "${aws_instance.server1.id}" allocation_id = "eipalloc-xxxxxxxx" }

To provision an instance, just run:

terraform apply

terraform apply

When you are done, just run:

terraform destroy

terraform destroy

Filed Under: Cloud Tagged With: aws, ec2, instance, provisioning, terraform

Create a Crontab via Script

December 30, 2018

How do you create a crontab entry via a Bash script? That’s a good question. Here is one solution:

(crontab -l 2>/dev/null; echo "* 11 * * * /path/to/job -with args") | crontab -

(crontab -l 2>/dev/null; echo "* 11 * * * /path/to/job -with args") | crontab -

If you want to run it as a specific user, use the following:

(crontab -l -u user 2>/dev/null; echo "* 11 * * * /path/to/job -with args") | crontab -

(crontab -l -u user 2>/dev/null; echo "* 11 * * * /path/to/job -with args") | crontab -

Filed Under: Linux Tagged With: bash, cron, user

WordPress on Kubernetes

December 28, 2018

Kubernetes has documentation on how to deploy WordPress on its platform. The setup is using Persistent Volumes for both WordPress and MySQL on MiniKube. Although the setup is not recommended for a production environment, it is a good start to get yourself familiar with Kubernetes. For production environments, they do recommend running WordPress on a Kubernetes cluster using the Helm package manager. Here’s the WordPress-MySQL deployment on MiniKube.

apiVersion: v1
kind: Service
metadata:
  name: wordpress-mysql
  labels:
    app: wordpress
spec:
  ports:
    - port: 3306
  selector:
    app: wordpress
    tier: mysql
  clusterIP: None
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
  labels:
    app: wordpress
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: wordpress-mysql
  labels:
    app: wordpress
spec:
  selector:
    matchLabels:
      app: wordpress
      tier: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: wordpress
        tier: mysql
    spec:
      containers:
      - image: mysql:5.6
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-pass
              key: password
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pv-claim

apiVersion: v1 kind: Service metadata: name: wordpress-mysql labels: app: wordpress spec: ports: - port: 3306 selector: app: wordpress tier: mysql clusterIP: None --- apiVersion: v1 kind: PersistentVolumeClaim metadata: name: mysql-pv-claim labels: app: wordpress spec: accessModes: - ReadWriteOnce resources: requests: storage: 20Gi --- apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2 kind: Deployment metadata: name: wordpress-mysql labels: app: wordpress spec: selector: matchLabels: app: wordpress tier: mysql strategy: type: Recreate template: metadata: labels: app: wordpress tier: mysql spec: containers: - image: mysql:5.6 name: mysql env: - name: MYSQL_ROOT_PASSWORD valueFrom: secretKeyRef: name: mysql-pass key: password ports: - containerPort: 3306 name: mysql volumeMounts: - name: mysql-persistent-storage mountPath: /var/lib/mysql volumes: - name: mysql-persistent-storage persistentVolumeClaim: claimName: mysql-pv-claim

Filed Under: Cloud, WP Tagged With: helm, kubernetes, minikube, wordpress

EC2 Delete Snapshots

December 27, 2018

Here’s the AWS CLI command for deleting a snapshot.

aws ec2 delete-snapshot \
--snapshot-id snap-xxxxxxxxxxxxxxx \
--profile default

aws ec2 delete-snapshot \ --snapshot-id snap-xxxxxxxxxxxxxxx \ --profile default

Filed Under: Cloud Tagged With: aws, cli, delete, snapshots

EC2 Describe Snapshots

December 27, 2018

If you have a ton of snapshots, they tend to be difficult to find within the AWS console. Although AWS has optimized the snapshot view, it’s still in beta. Filtering a volume id is still a problem. It might be faster just to use the AWS CLI to display snapshot information. Here’s an example of how to display snapshots given a certain volume id.

aws ec2 describe-snapshots \
--filters Name=volume-id,Values=vol-xxxxxxxxxxxxxx \
--query "Snapshots[*].{ID:SnapshotId,Time:StartTime,Progress:Progress}" \
--profile default

aws ec2 describe-snapshots \ --filters Name=volume-id,Values=vol-xxxxxxxxxxxxxx \ --query "Snapshots[*].{ID:SnapshotId,Time:StartTime,Progress:Progress}" \ --profile default

The result is something like this.

{
        "Progress": "99%",
        "ID": "snap-xxxxxxxxxxxxxxx",
        "Time": "2018-12-25T10:19:51.385Z"
    },
    {
        "Progress": "99%",
        "ID": "snap-xxxxxxxxxxxxxxx",
        "Time": "2018-12-24T09:35:09.357Z"
    }

{ "Progress": "99%", "ID": "snap-xxxxxxxxxxxxxxx", "Time": "2018-12-25T10:19:51.385Z" }, { "Progress": "99%", "ID": "snap-xxxxxxxxxxxxxxx", "Time": "2018-12-24T09:35:09.357Z" }

Filed Under: Cloud Tagged With: aws, cli, snapshots, volumes

Building A Custom Block

December 26, 2018

I found a new WordPress plugin that allows you to add custom blocks within the Gutenberg editor. You can create any block that you want by using the same templating code you’re already familiar with in WordPress. You can work with CSS, PHP, plugin shortcodes, etc. It’s a beautiful plugin with endless possibilities. The plugin is called Block Lab.

Filed Under: WP Tagged With: block lab, custom blocks, editor, gutenberg, wordpress

Sublime Text Comment Multiple Lines

December 26, 2018

If you need to comment out multiple lines in Sublime Text 2 or 3, there’s a short keystroke that you can use. First, you need to highlight the block (multiple lines) that you want to comment out, then press Ctrl + /. You can press it again to toggle on and off.

Line 1
Line 2
Line 3

Line 1 Line 2 Line 3

Pressing Ctrl + / will comment out the highlighted block.

#Line 1
#Line 2
#Line 3

#Line 1 #Line 2 #Line 3

Press Ctrl + / again to toggle it back.

Filed Under: Misc Tagged With: comments, editor, sublime text

AWS EFS Userdata

December 25, 2018

If you want to auto mount the EFS volumes when the instance is created, add the following script to userdata:

#cloud-config
repo_update: true
repo_upgrade: all
 
packages:
- amazon-efs-utils
 
runcmd:
- file_system_id_01=fs-12345678
- file_system_id_02=fs-34593405
- efs_directory_01=/mnt/efs
- efs_directory_02=/mnt/efs2
 
- mkdir -p ${efs_directory_01}
- mkdir -p ${efs_directory_02}
 
- echo "${file_system_id_01}:/ ${efs_directory_01} efs tls,_netdev" >> /etc/fstab
- echo "${file_system_id_02}:/ ${efs_directory_02} efs tls,_netdev" >> /etc/fstab
 
- mount -a -t efs defaults

#cloud-config repo_update: true repo_upgrade: all packages: - amazon-efs-utils runcmd: - file_system_id_01=fs-12345678 - file_system_id_02=fs-34593405 - efs_directory_01=/mnt/efs - efs_directory_02=/mnt/efs2 - mkdir -p ${efs_directory_01} - mkdir -p ${efs_directory_02} - echo "${file_system_id_01}:/ ${efs_directory_01} efs tls,_netdev" >> /etc/fstab - echo "${file_system_id_02}:/ ${efs_directory_02} efs tls,_netdev" >> /etc/fstab - mount -a -t efs defaults

Just add more if you have more mounts.

Filed Under: Cloud Tagged With: auto, aws cli, efs, mount

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

Copyright © 2023