SELECT * FROM iasql_install('<modules>');
INSERT INTO iam_role (role_name, assume_role_policy_document, attached_policies_arns)
VALUES ('<codePipelineRoleName>', '<assumeServicePolicy>', array['<codepipelinePolicyArn>', '<s3PolicyArn>', '<codedeployPolicyArn>', '<globalIamPolicyArn>', '<cloudformationPolicyArn>']);
INSERT INTO iam_role (role_name, assume_role_policy_document, attached_policies_arns)
VALUES ('<codePipelineCfRoleName>', '<assumeServicePolicy>', array['<codepipelinePolicyArn>', '<s3PolicyArn>', '<codecommitPolicyArn>', '<codedeployPolicyArn>', '<globalIamPolicyArn>', '<cloudformationPolicyArn>']);
INSERT INTO iam_role (role_name, assume_role_policy_document, attached_policies_arns)
VALUES ('<cloudformationRoleName>', '<cloudformationRolePolicy>', array['<codedeployPolicyArn>', '<globalIamPolicyArn>', '<cloudformationPolicyArn>']);
INSERT INTO iam_role (role_name, assume_role_policy_document, attached_policies_arns)
VALUES ('<ec2RoleName>', '<ec2RolePolicy>', array['<deployEC2PolicyArn>', '<ssmPolicyArn>', '<codedeployPolicyArn>', '<s3PolicyArn>']);
INSERT INTO iam_role (role_name, assume_role_policy_document, attached_policies_arns)
VALUES ('<codeDeployRoleName>', '<codedeployRolePolicy>', array['<codedeployPolicyArn>', '<deployEC2PolicyArn>', '<s3PolicyArn>']);
INSERT INTO bucket (name) VALUES ('<bucket>');
INSERT INTO bucket (name) VALUES ('<testLambdaBucketName>');
INSERT INTO pipeline_declaration (name, service_role_name, stages, artifact_store)
SELECT '<prefix>-lambda-<dbAlias>', '<codePipelineCfRoleName>',
CAST(REPLACE('<lambdaStages>', '##ROLE_ARN##', arn) AS json), '<artifactStore>' FROM iam_role WHERE role_name='<cloudformationRoleName>';
SELECT * FROM pipeline_declaration
WHERE name = '<prefix>-lambda-<dbAlias>';
INSERT INTO security_group (description, group_name)
VALUES ('CodedeploySecurity Group', '<sgGroupName>');
INSERT INTO security_group_rule (is_egress, ip_protocol, from_port, to_port, cidr_ipv4, description, security_group_id)
SELECT false, 'tcp', 22, 22, '0.0.0.0/0', '<prefix>codedeploy_rule_ssh', id
FROM security_group
WHERE group_name = '<sgGroupName>';
INSERT INTO security_group_rule (is_egress, ip_protocol, from_port, to_port, cidr_ipv4, description, security_group_id)
SELECT false, 'tcp', 80, 80, '0.0.0.0/0', '<prefix>codedeploy_rule_http', id
FROM security_group
WHERE group_name = '<sgGroupName>';
INSERT INTO security_group_rule (is_egress, ip_protocol, from_port, to_port, cidr_ipv4, description, security_group_id)
SELECT true, 'tcp', 1, 65335, '0.0.0.0/0', '<prefix>codedeploy_rule_egress', id
FROM security_group
WHERE group_name = '<sgGroupName>';
INSERT INTO codedeploy_application (name, compute_platform)
VALUES ('<applicationNameForDeployment>', 'Server');
INSERT INTO codedeploy_deployment_group (application_id, name, role_name, ec2_tag_filters)
VALUES ((SELECT id FROM codedeploy_application WHERE name = '<applicationNameForDeployment>'), '<deploymentGroupName>', '<codeDeployRoleName>', '<ec2FilterTags>');
INSERT INTO pipeline_declaration (name, service_role_name, stages, artifact_store)
VALUES ('<prefix>-<dbAlias>', '<codePipelineRoleName>', '<stages>', '<artifactStore>');
SELECT * FROM pipeline_declaration
WHERE name = '<prefix>-<dbAlias>';
DELETE FROM pipeline_declaration
WHERE name IN ('<prefix>-<dbAlias>', '<prefix>-buggy-<dbAlias>', '<prefix>-lambda-<dbAlias>');
DELETE FROM codedeploy_deployment_group
WHERE name = '<deploymentGroupName>';
DELETE FROM codedeploy_application
WHERE name = '<applicationNameForDeployment>';