Added example and KB to create a free ampere instance in Oracle Cloud

This commit is contained in:
Michele Cereda
2023-01-08 19:14:35 +01:00
parent ad3ceedd17
commit 5e6de46733
10 changed files with 264 additions and 0 deletions

2
.gitignore vendored
View File

@@ -8,5 +8,7 @@
.terraform/
.terraform.lock.hcl
*.auto.tfvars
*.tfstate
*.tfstate.backup
__pycache__/

60
knowledge base/oci-cli.md Normal file
View File

@@ -0,0 +1,60 @@
# OCI CLI
Oracle Cloud Infrastructure CLI.
1. [TL;DR](#tldr)
2. [Configuration](#configuration)
3. [Further readings](#further-readings)
## TL;DR
```sh
# Install the CLI.
brew install 'oci-cli'
# Start the interactive setup.
oci setup config
# Generate a key pair to include in the config file.
oci setup keys
# Show the current configuration.
cat ~/.oci/config
# List available compartments.
oci iam compartment list
oci iam compartment list -c 'tenancy_id'
# Create compartments.
oci iam compartment create -c 'root_compartment_id' \
--name 'compartment_name' --description 'friendly_description'
# List available availability domains.
oci iam availability-domain list
oci iam availability-domain list -c 'tenancy_id'
# List available compute images.
# Output is paginated.
oci compute image list -c 'tenancy_id' --all
oci compute image list -c 'tenancy_id' \
--operating-system 'Oracle Linux' --operating-system-version '8' \
--lifecycle-state 'AVAILABLE'
```
## Configuration
| Unix location | Description |
| ------------------------ | -------------------------------------------------------------------------------------- |
| `~/.oci/config` | The default configuration file. |
| `~/.oci/oci_api_key.pem` | Full path and filename of the private key. The key pair **must be in the PEM format**. |
## Further readings
- [Command Line Interface]
- [SDK and CLI Configuration File]
- [Required keys and OCIDs]
<!-- oracle cloud's documentation -->
[command line interface]: https://docs.oracle.com/en-us/iaas/Content/API/Concepts/cliconcepts.htm
[required keys and ocids]: https://docs.oracle.com/en-us/iaas/Content/API/Concepts/apisigningkey.htm
[sdk and cli configuration file]: https://docs.oracle.com/en-us/iaas/Content/API/Concepts/sdkconfig.htm

View File

@@ -0,0 +1,34 @@
# Oracle Cloud
1. [Concepts](#concepts)
1. [Compartments](#compartments)
2. [Further readings](#further-readings)
3. [Sources](#sources)
## Concepts
### Compartments
Compartments are tenancy-wide and extend across regions. They can also be nested to create hierarchies up to 6 levels deep.
After creating a compartment, you need to write at least one policy for it; until then, no one can access it except administrators or users who have permissions set at the tenancy level. When creating sub-compartments, they inherit access permissions from compartments higher up their hierarchy.
Before deleting a compartment, all its resources must have been moved, deleted or terminated, including any policies attached to the compartment itself.
## Further readings
- [oci-cli]
- [compute images]
## Sources
- [Required keys and OCIDs]
<!-- oracle cloud's documentation -->
[compute images]: https://docs.oracle.com/en-us/iaas/images/
[required keys and ocids]: https://docs.oracle.com/en-us/iaas/Content/API/Concepts/apisigningkey.htm
<!-- internal references -->
[oci-cli]: ./oci-cli.md
<!-- external references -->

View File

@@ -0,0 +1,37 @@
# Oracle free tier Ampere VM
Simple example to create an Ampere VM instance in Oracle Cloud's free tier.
1. [Requirements](#requirements)
2. [Further readings](#further-readings)
3. [Sources](#sources)
## Requirements
1. VCN
1. Public Subnet
For a Subnet to be considered Public, it needs to have associated a Route Table with a default route pointing to an Internet Gateway.
The default route table created using Terraform does not contain this route, nor it is possible to create the single route in it at the time of writing.<br />
A solution to this is to create a new Route Table **with** the default route above and attach it to the Subnet. See the code for details.
![requirements]
## Further readings
## Sources
- [Ridiculously powerful free server in the cloud]
- [Always free resources] in Oracle Cloud
- [Oracle Cloud Infrastructure Provider documentation]
- [oracle-terraform-modules/terraform-oci-compute-instance]
<!-- internal references -->
[requirements]: design/requirements.png
<!-- external references -->
[always free resources]: https://docs.oracle.com/en-us/iaas/Content/FreeTier/freetier_topic-Always_Free_Resources.htm
[oracle cloud infrastructure provider documentation]: https://registry.terraform.io/providers/oracle/oci/latest/docs
[ridiculously powerful free server in the cloud]: https://medium.com/codex/ridiculously-powerful-free-server-in-the-cloud-dd4da8524a9c
[oracle-terraform-modules/terraform-oci-compute-instance]: https://github.com/oracle-terraform-modules/terraform-oci-compute-instance

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

View File

@@ -0,0 +1,18 @@
#!/usr/bin/env python3
from diagrams import Cluster, Diagram
from diagrams.oci.compute import VM
from diagrams.oci.network import InternetGateway, RouteTable, Vcn
with Diagram("Requirements", show=False):
vcn = Vcn("VCN")
vm = VM("Ampere instance")
with Cluster("Public Subnet"):
ig = InternetGateway("Internet Gateway")
rt = RouteTable("Route Table")
vcn >> ig >> rt
rt >> vm

View File

@@ -0,0 +1,14 @@
availability_domain = "FIXME" # get it with `oci iam availability-domain list`
compartment_id = "FIXME" # get it with `oci iam compartment list`
memory_in_gbs = 24
ocpus = 4
# get it with `oci compute image list -c 'tenancy_id'`
# or check https://docs.oracle.com/en-us/iaas/images/
source_id = "ocid1.image.oc1.eu-amsterdam-1.aaaaaaaavmra3s4va4fqd4vlcrqc5v5jyqov5vdla3x3b6gzc64n6dkpuqua"
ssh_authorized_keys = <<EOT
ssh-ed25519 key-1 comment
ssh-ed25519 key-n comment
EOT

View File

@@ -0,0 +1,61 @@
# https://registry.terraform.io/providers/oracle/oci/latest/docs/resources/core_vcn
resource "oci_core_vcn" "this" {
compartment_id = var.compartment_id
cidr_blocks = ["10.0.0.0/16"]
}
# https://registry.terraform.io/providers/oracle/oci/latest/docs/resources/core_subnet
resource "oci_core_subnet" "this" {
compartment_id = var.compartment_id
vcn_id = oci_core_vcn.this.id
cidr_block = "10.0.0.0/24"
}
# Needed to be able to connect to the instance from the Internet.
# Need to create a route table with the default route 0.0.0.0/0 pointing to the
# internet gateway, and associate the subnet to it.
# https://registry.terraform.io/providers/oracle/oci/latest/docs/resources/core_internet_gateway
resource "oci_core_internet_gateway" "this" {
compartment_id = var.compartment_id
vcn_id = oci_core_vcn.this.id
}
resource "oci_core_route_table" "this" {
compartment_id = var.compartment_id
vcn_id = oci_core_vcn.this.id
route_rules {
destination = "0.0.0.0/0"
destination_type = "CIDR_BLOCK"
network_entity_id = oci_core_internet_gateway.this.id
}
}
resource "oci_core_route_table_attachment" "this" {
subnet_id = oci_core_subnet.this.id
route_table_id = oci_core_route_table.this.id
}
# https://registry.terraform.io/providers/oracle/oci/latest/docs/resources/core_instance
resource "oci_core_instance" "this" {
compartment_id = var.compartment_id
availability_domain = var.availability_domain
shape = var.shape
create_vnic_details {
subnet_id = oci_core_subnet.this.id
}
metadata = {
ssh_authorized_keys = var.ssh_authorized_keys
}
shape_config {
memory_in_gbs = var.memory_in_gbs
ocpus = var.ocpus
}
source_details {
boot_volume_size_in_gbs = var.boot_volume_size_in_gbs
source_id = var.source_id
source_type = var.source_type
}
}

View File

@@ -0,0 +1,3 @@
output "instance" {
value = oci_core_instance.this
}

View File

@@ -0,0 +1,35 @@
variable "availability_domain" {
type = string
}
variable "compartment_id" {
type = string
}
variable "shape" {
type = string
default = "VM.Standard.A1.Flex"
}
variable "memory_in_gbs" {
type = number
default = 24
}
variable "ocpus" {
type = number
default = 4
}
variable "boot_volume_size_in_gbs" {
type = number
default = 50
}
variable "source_id" {
type = string
}
variable "source_type" {
type = string
default = "image"
}
variable "ssh_authorized_keys" {
type = string
}