Giter VIP home page Giter VIP logo

cfn-mysql-user-provider's Introduction

cfn-mysql-user-provider

Although CloudFormation is very good in creating MySQL database servers with Amazon RDS, the mundane task of creating users and database is not supported. This custom MySQL user provider automates the provisioning of MySQL users and databases.

How does it work?

It is quite easy: you specify a CloudFormation resource of the Custom::MySQLUser, as follows:

  KongUser:
    Type: Custom::MySQLUser
    DependsOn: KongPassword
    Properties:
      User: kong
      PasswordParameterName: /MySQL/kong/PGPASSWORD
      WithDatabase: true
      DeletionPolicy: Retain 
      Database:                   # the server to create the new user or database in
        Host: MySQL
        Port: 3306
        DBName: root
        User: root
        PasswordParameterName: /MySQL/root/PGPASSWORD                # put your root password is in the parameter store
      ServiceToken: !Sub 'arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:binxioio-cfn-mysql-user-provider-vpc-${AppVPC}'

   KongPassword:
    Type: Custom::Secret
    Properties:
      Name: /MySQL/kong/PGPASSWORD
      KeyAlias: alias/aws/ssm
      Alphabet: _&`'~-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
      Length: 30
      ServiceToken: !Sub 'arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:binxio-cfn-secret-provider'

After the deployment, the MySQL user 'kong' has been created together with a matching database 'kong'. The password for the root database user has been obtained by querying the Parameter /MySQL/root/PGPASSWORD. If you just want to create a user with which you can login to the MySQL database server, without a database, specify WithDatabase as false.

The DeletionPolicy by default is Retain. This means that the login to the database is disabled. If you specify drop, it will be dropped and your data will be lost.

Installation

To install this Custom Resource, type:

export VPC_ID=$(aws ec2  --output text --query 'Vpcs[?IsDefault].VpcId' describe-vpcs)
export SUBNET_ID=$(aws ec2 --output text --query 'Subnets[0].SubnetId' \
			describe-subnets --filters Name=vpc-id,Values=$VPC_ID)
export SG_ID=$(aws ec2 --output text --query 'SecurityGroups[*].GroupId' \
			describe-security-groups --group-names default  --filters Name=vpc-id,Values=$VPC_ID)

aws cloudformation create-stack \
	--capabilities CAPABILITY_IAM \
	--stack-name cfn-mysql-user-provider \
	--template-body file://cloudformation/cfn-resource-provider.yaml  \
	--parameters \
	            ParameterKey=VPC,ParameterValue=$VPC_ID \
	            ParameterKey=Subnets,ParameterValue=$SUBNET_ID \
                    ParameterKey=SecurityGroup,ParameterValue=$SG_ID

aws cloudformation wait stack-create-complete  --stack-name cfn-mysql-user-provider 

Note that this uses the default VPC, subnet and security group. As the Lambda functions needs to connect to the database. You will need to install this custom resource provider for each vpc that you want to be able to create database users.

This CloudFormation template will use our pre-packaged provider from s3://binxio-public/lambdas/cfn-mysql-user-provider-1.0.1.zip.

If you have not done so, please install the secret provider too.

cd ..
git clone https https://github.com/binxio/cfn-secret-provider.git 
cd cfn-secret-provider
aws cloudformation create-stack \
	--capabilities CAPABILITY_IAM \
	--stack-name cfn-secret-provider \
	--template-body file://cloudformation/cfn-custom-resource-provider.yaml
aws cloudformation wait stack-create-complete  --stack-name cfn-secret-provider 

Demo

To install the simple sample of the Custom Resource, type:

aws cloudformation create-stack --stack-name cfn-mysql-user-provider-demo \
	--template-body file://cloudformation/demo-stack.yaml
aws cloudformation wait stack-create-complete  --stack-name cfn-mysql-user-provider-demo

It will create a MySQL database too, so it is quite time consuming...

Conclusion

With this solution MySQL users and databases can be provisioned just like the RDS instance, while keeping the passwords safely stored in the AWS Parameter Store.

cfn-mysql-user-provider's People

Contributors

henrycarteruk avatar majdarbash avatar mtsr avatar mvanholsteijn avatar omry-hay avatar rhertogh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

cfn-mysql-user-provider's Issues

Stuck in CREATE_IN_PROGRESS / DELETE_IN_PROGRESS

Hello there,

First of all I'm quite excited to see there is a custom resource plugin for cloudformation to create MySQL DB users. Thanks for all the work and open sourcing this great stuff!

While the documentation seems thorough, I'm experiencing an issue while trying out, and wondering if I'm missing out on some key steps.

I'm trying to deploy the demo, and noticed that it is stuck in CREATE_IN_PROGRESS when creating KongReaderUser and KongUser.

2019-09-08 22:36:15 UTC+0800 | KongReaderUser | CREATE_IN_PROGRESS | -
2019-09-08 22:36:15 UTC+0800 | KongUser | CREATE_IN_PROGRESS

I have installed both the user-provider and secret-provider (otherwise I wouldn't be able to reach the above).

To minimize dependencies, I have also tried to remove secret-provider in the demo template, and hard-coded the passwords in. However, it was still stuck in the same place.

Then, when I gave up, and tried to delete the cloudformation stack, the status was stuck in DELETE_IN_PROGRESS for the above users as well, and then finally failed after an hour.

2019-09-09 00:20:56 UTC+0800 KongReaderUser DELETE_FAILED Custom Resource failed to stabilize in expected time
2019-09-09 00:20:53 UTC+0800 KongUser DELETE_FAILED Custom Resource failed to stabilize in expected time
2019-09-08 23:20:40 UTC+0800 KongUser DELETE_IN_PROGRESS -
2019-09-08 23:20:40 UTC+0800 KongReaderUser DELETE_IN_PROGRESS

I tried to look in CloudWatch, but it doesn't show any errors. I'm not an expert in Python, so what I can understand is limited. But I did notice a few functions have try and finally, but no "except".

Could it be hanging in some place, with no exceptions? Could anyone give me a hand and shed me some light, as I'm quite lost at the moment.

Thank you very much guys in advance!

Problem to create user

Hello,

I would like to use this fantantic solution, but I could not create the MySQL user. I try with the template demo and it has not worked for me.

Lambda log:

START RequestId: c5004a56-4cdd-45bf-932c-375e687d921b Version: $LATEST

[INFO] 2020-12-10T15:52:52.131Z Found credentials in environment variables.
[INFO] 2020-12-10T15:52:52.195Z c5004a56-4cdd-45bf-932c-375e687d921b connecting to database cdlywf1eukm952.cikl3bobkn44.us-east-1.rds.amazonaws.com on port 3306 as user root
END RequestId: c5004a56-4cdd-45bf-932c-375e687d921b
REPORT RequestId: c5004a56-4cdd-45bf-932c-375e687d921b Duration: 3003.15 ms Billed Duration: 3000 ms Memory Size: 128 MB Max Memory Used: 83 MB Init Duration: 449.17 ms
2020-12-10T15:52:55.195Z c5004a56-4cdd-45bf-932c-375e687d921b Task timed out after 3.00 seconds
[INFO] 2020-12-10T15:52:56.513Z Found credentials in environment variables.

Thank you!!!

Regards.

Missing template-body cfn-custom-resource-provider.json

Hi, I'm following the steps in the readme willing to setup the mysql user provider, but I noticed that the documentation mentions --template-body file://cloudformation/cfn-custom-resource-provider.json but I could not find this file in the project or any other reference to it.

How does it works to generate/gather this file?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.