Hi @all, how are you going ? Mess up, and not have mood to write anything because kind of stuff in social life. But as you can see, I must to do for probably myself to continuous contribute, so here I am. Today, I want to guide you about how we can implement own Terraform for yourself, distribute and see what how far you adapt with that for project. Letβs digest with target AWS SSO
AWS SSO
As usual, before starting anything, we need to learn about tech stack, and AWS SSO (IAM Identity Center) is super cool, and really helpful when you think about solution for authorization and authentication for AWS Account and profiles
Introduce
Explore about the definition and benefit of AWS SSO, through those articles
AWS IAM Identity Center is the recommended service for managing your workforceβs access to AWS applications, such asΒ Amazon Q Developer. It is a flexible solution that can be used to connect your existing identity source once andΒ gives your AWS applications a common view of your users. Your users get a streamlined, consistent experience across AWS applications. It works alongside existing AWS account access configurations.
With many features of AWS Identity Center, you will receive some excited benefit
Connect your existing identity source to streamline accessing AWS
Efficiently manage workforce access to AWS applications
Improve control and visibility of user access to data in AWS applications
Manage workforce access to a multi-account AWS environment
Why do we need AWS SSO ?
Warning
The factor to choose SSO or barely method is totally depend on your strategy which one apply for whole AWS Account and applications
Tactically, Bare IAM brings a fast way to access your account, but if organization does expand, it means your AWS Cloud has more than two accounts, the problem is now coming up in managing username and password.
Next, the truth about bare IAM has a way to switch account with only account, but you need handle to control and access right account to switch between account in organization with specific role and permission, and ainβt gonna easy for this stuff.
Big problem, when you scale up your organization, you canβt control over user, permission and itβs bringing up non-consistency and cause damage in secure for your organization.
Info
That why here the opportunity to AWS SSO become potential solution for adapting with any AWS organization, not only about techniques, secure but also the solution will bring more efficiency for manage AWS, costs and many reason to think about exchange. Explore more use cases to digest with SSO
If you use AWS SSO, you can have
A single URL which is the gateway to all accounts in your organization.
A directory in which you can assign usernames and passwords to users.
A configuration which clearly maps users to groups, and maps groups to AWS accounts and permissions.
Note
You can see and control over mange activities of user access in AWS portal, or CLI for what ever but bare IAM can but not clearly than SSO, honestly.
How to enable AWS SSO
You need to access to AWS Management Account, It means you need to access the root account which supervise whole your AWS organization, for example
Now reach to search box, and find IAM Identity Center
Now click Enable button to enabling service
Let reach to second part, this is only step you need to handle manual on AWS Portal, and all of others step can covered by Terraform
Terraform Module Registry
If you go there, you ensure familiar with terraform, you can double check couple my blogs about this technology
As you can see, terraform is using many block called module, It means whole provisioning progress is encapsulating inside the module, and exposing interface to help us call from remote, locally or what ever purpose you want
Question
The story begins about you need to figure out what place can help you keep that module, and provide the methodology to any people can try to pull and retrieve with your release remotely module, and Using Terraform Registry is one of the best solution of community
We have both solution open-source and profit to handle that, explore at Module Sources. There are many potential selection, such as
Terraform Registry (Free for community, Limited for release module ~500 module)
Github and BitBucket (Free but not good idea for store module)
Furthermore, there are candidates stand out there, for free and private registry purpose tapir, Terrareg, β¦ Read article and awesome-tf to exploring more
Note
In my perspective, you have more than one tool to hand on with purpose creating own registry, and those one will create competitive environment but honestly, I prefer to use Gitlab with automation release, gitops and lot of stuff to you when try to do implement Terraform in this platform
Make for you own
Question
In this practice session of week, we will try to combine with creating AWS SSO module, building this one becomes public module, releasing with Gitlab Terraform Registry and reusing to enhance in your AWS Account.
First of all start, I want to send my shout-out for people and community who inspired me this idea
This build is not the first appearance, but with me this is first time I try to handle the practicing and I appreciate about it, totally experience
I will list the step need to do, and we will try to walk through to get best result of practice session
Use Terraform with Gitlab provider to create template module with dynamically, you can use with Github or BitBucketβ You will have structure of whole all terraform-module, e.g terraform-example-module and generate with .prehook and terraform-docs
Setup the pipeline to generate module in Gitlab to contribute your module
Reuse with your own implement
Play with AWS SSO
Create a Terraform module template structure
With the inspiration, we are using Gitlab Provider to handling all of stuff, you just need to create PAT Token of Gitlab, and can digest with implement Terraform
To create PAT Token, you can follow this documentation with permission is API because you Terraform providers will call API to create your repository inside your account
Now you are having a token, next head to implement terraform and see what you have. First of all, let take a look the structure of terraform repositories
Afterward, I will go with you to detailing what you need to write inside those files
First of all, you need to add .gitignore for terraform project, and this work become important to prevent leak any data terraform. You can use .gitignore.io to create one for yourself
Next we reach to .pre-commit, so Itβs really framework to help you implement the process before you commit your change into git. Explore more about .pre-commit, and with terraform .pre-commit does help you to formatting, and generate docs which provide a such great information with terraform-docs for your terraform repositories
To install pre-commit and terraform-docs, you can run
And now definition .pre-commit configuration file
You can perform pre-commit install to query and install hook for your .git
So your .git will trigger that command after you try to run git commit to your upstream, next we will implement with *.tf to figure out what need to setup. Just a simple thing
If you can see examples directory, it just a example which create to use module, but just except it. For working terraform-docs, they recommend us to use configuration-file for working with this technology. You need to add .terraform-docs.yml into your example repository
This configuration file will generate information about your terraform module into README.md, and this will work cut off time for write module info, integrating into .pre-commit to prevent miss actions π
Alright, you can push that into your gitlab and take this for example template with one, and see result when run .pre-commit, okay
Next, we are moving on to use terraform with Gitlab provider to provisioning the multiple project base on template
Structure of terraform-gitlab-supply will include smt like this
Let go to detail of each components, keep the same thing with .pre-commit-config.yaml file
Conclusion
Create aws-modules base on local.aws_modules
Provide repo_name and description into for_each
Use from module ./modules
Conclusion
Keep .tfstate in the local machine
Import provider gitlabhq/gitlab
You need to provide add-on one file variables.tf to supply variable for gitlab with token
Next we head to provisioning inside module directory
Conclusion
Supply local to set the name inside gitlab_project resource
prevent_destroy is enable to help you hard to destroy resource
Info
If you wonder why we need to put that gitlab provider in this location, the reason why gitlab provider is not come from hashicorp and it means when you try to run terraform init, absolutely you will receive the error because you are using provider partner of Terraform
And when you add the definition about gitlabhq/gitlab, your error is gone
Now run terraform workflow to get the result in creation repositories inside your gitlab
But remember you have GITLAB_TOKEN to setting, many method you can use to handle it
Prompt the result when terraform ask you (Itβs on you bruh π)
To not miss, I try to add teraform.tfvars into root terraform project directory
Now run plan to get the output result
Check and validate, now run apply to get the result, and error back to reason they tell about we need group to exchange our template become customize template, issue link. So we create group and move your repository to group at Tutorial: Move your personal project to a group
Next, we need add sub-group where you store module because if you miss this stuff, that gonna error continuous
Alright, you modify your gitlab.tf and we gonna ready to create repository
Apply again, and your project is created
Provision AWS SSO module for supplying
Like I told about plan, we need to create module and inject into our organization remotely, and here is the thing we should handle in this practice session.
First of all, the module will take structure like terraform-module-example, because itβs clone π. But the difference about it use for implement AWS SSO module, and this code is mostly depend on iam-identity-center and itβs supply incredible feature, including
Create a new group SSO with dynamic functionality
Create a new user SSO with dynamic functionality
Create a Group membership with dynamic functionality
Create an SSO Permission Set with dynamic functionality
We will move to detail for whole module
And now you have done setup and implement entire the aws-iam-sso module with providing us those features above.
Create pipeline to generate module in Gitlab
As you remember in top of this topic, I just relate about gitlab where is a good place to put your module as remotely with same purpose with terraform registry and totally free. Therefore, I will guide you to handle that one. Read again documentation at Terraform Module Registry and you have multiple method to handle that
Following the documentation, they recommend us to using ci/cd and I think that great idea because we can cut off the time with template is build before from gitlab team
To handle pipeline, you need to add .gitlab-ci.yml into your root terraform module
See, that look is very easily because whole step is writing from template of gitlab, you can find out the method to handle that via Use template with GitlabCI blog. The list of steps write inside these file (Take a look)
You can choose one of theme and version I use in this practice with first option, because that first template is depend on second template, make them clear but get the fully option. But remember, you have four variables that can exchange to make sure that compatible with your release, including
TERRAFORM_MODULE_DIR (Default: ${CI_PROJECT_DIR}) - The relative path to the root directory of the Terraform project.
TERRAFORM_MODULE_NAME (Default: ${CI_PROJECT_NAME}) - The name of your Terraform module. Must not contain any spaces or underscores.
TERRAFORM_MODULE_SYSTEM (Default: local) - The system or provider of your Terraform module targets. For example, local, aws, google.
TERRAFORM_MODULE_VERSION (Default: ${CI_COMMIT_TAG}) - The Terraform module version. You should follow the semantic versioning specification.
Pipeline will split into three step - with two scenarios
Info
3 Steps
fmt - Validate the formatting of the Terraform module.
kics-iac-sast - Test the Terraform module for security issues.
deploy - For tag pipelines only. Deploy the Terraform module to the Terraform Module Registry.
2 scenarios
Merge from branch to mainβ Trigger kisc-iac-sats job
Only mainβ Trigger kisc-iac-sats for branch β Trigger deploy for tag version
Technically, Itβs really complex right, but you need to make familiar with this stuff, and practice with gitlab is super fun.
If you notice about deploy description, that tell you about tag, and it forces you to use tag to release your module to trigger deploy π. I think you should need it to handling this stuff
I will run slow step by step to helping you figure out what need to on next time
First of all, plz add the .pre-commit to not mizz anything else before commit π
Secondly, I choose scenario 2, therefore, I push directly commit to main branch to run deploy and first step you need to create commit with include whole of change
Info
Remember, you add .precommit hook in your project, so you need to validate all stuff and .prehook will make a change to help you reduce step you skip, therefore you just need to preform twice command again to reduce to bypass all step
Thirdly, You need to set the tag for your release, through command
After check and validate, now you need push your commit with tag to remote branch
Go to gitlab and see the result, now
In some situation, you will meet this problem in release
Warning
If you meet some stuff about git tag, read this issue to help you rename and remove tag or update old to new
Info
Because I set v0.0.1 instead of for 0.0.1 and make fail for module, some situation your release in gitlab with not work for module version v0.0.1, therefore just fix my mistake
And check again
Successfully, now you can review your module with choosing Operate β Terraform modules
Info
You got it, your own aws module and contribute for your teammate ππ
Reuse with your AWS SSO module
To reuse this module, first of all you need to configure your terraform with retrieve your Gitlab PAT inside .terraformrc
First of all, create ~/.terraformrc in your Mac/Linux machine or %APPDATA%/terraform.rc in your windows machine
Now edit that with content
Alright, now you can reuse this module from your local machine π₯Άπ₯Ά. Now we try to define it with
Go to IAM of your aws account, generate access_token with admin permission and export that into machine environment variable
And check with command
After that, you just need to trigger terraform workflow to get the result
Back to IAM Identity Center in AWS portal, you can see sso user is adding to
Play with AWS SSO
Now first of all you need to retrieve the portal of SSO, you need head to homepage of IAM identity center and take the look in Settings summary part, you can see AWS access portal URL (NOTE: edit if you want)
Now access to AWS SSO Portal, you will have the UI like this
In this time, you need to request DevOps or Root account in AWS export to your pass to first login into this portal, but remember
Warning
Send the email for SSO user to validate account that before receive the best feature from AWS SSO
Now, you are having two plan for reset password for user
Click Reset Password button in AWS portal, and send email reset password or generate one time password (NOTE: This password will provide and change after user login into the system)
User need click to forgot password in AWS SSO Portal, and send the email for reset the password
After your user login into SSO, you need to add-on authentication method with theses option
With me Authentication App is the best option, but if you have multiple device for other option, go for it and receive more protection, 100%
And when you add authentication successfully, you will head on to AWS access portal with provide the best option to hand on with AWS Account
Info
When you try to click to *Access, you will head up to AWS Portal inside this account with permission you give for user, and with Access keys that give you dynamically access keys to connect to your AWS Account with no need generate or configure for yourself
Delete SSO service
To bring down AWS SSO, you just need to run terraform to destroy user, group and permission inside
Disable your AWS SSO in region by doing with step in documentation
On theΒ SettingsΒ page, choose theΒ ManagementΒ tab.
In theΒ Delete IAM Identity Center configurationΒ section, chooseΒ Delete.
In theΒ Delete IAM Identity Center configurationΒ dialog, select each of the check boxes to acknowledge you understand that your data that will be deleted. Type your IAM Identity Center instance in the text box, and then chooseΒ Confirm.
Conclusion
Success
This is all for this weekend, hope you feel great with adventure to hand on with AWS SSO and provide supply methodology to contribute your terraform module for your own. This story can come so long, but so sorry I think it is enough and more things I wonder you need to get from this. But I recommend you try to fail, and get the experience for yourself and AWS SSO is a one of top solution for modern AWS managing π¬
Quote
This week is really tough, and gonna say about I donβt have mood to release anything else, but for my savage, I want to continue contribute for my community, you are such a great things to help me stand up and think every day. Therefore, plz stay safe, learn new something and I will see yah next weekend. Bye bye π